yii框架怎么配置pathinfo的模式
第一次部署好Yii框架搭建的應用后,框架默認使用的不是PathInfo形式的URL,而是類似http://yourdomain.com/index.php?r=account/login 這樣的形式,這種URL不僅不美觀,而且不利于SEO,所以下面介紹在Yii中如何使用PathInfo形式的URL(注:開發(fā)環(huán)境基于wampserver2.4)。
1)打開protected/config/main.php配置文件,將下面這段urlManager代碼的注釋去掉:
'urlManager' => array( 'urlFormat' => 'path', 'rules' => array( '<controller:w+>/<id:d+>'=>'<controller>/view', '<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action>', '<controller:w+>/<action:w+>'=>'<controller>/<action>', ), ),
2)去掉以后,我們就可以使用類似http://yourdomain.com/index.php/controller/action這種形式的URL去訪問應用,但是接下來我們還要隱藏掉中間的那個index.php;
相關文章教程推薦:yii教程
3)在應用的根目錄下添加一個名為.htaccess的文件,并寫入以下內容:
Options +FollowSymLinks IndexIgnore */* RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
4)開啟apache的rewrite模塊,在httpd.conf中找到#LoadModule rewrite_module modules/mod_rewrite.so,把前面的“#”去掉;
5)重啟apache;
6)繼續(xù)編輯main.php文件,在剛才那個urlManager的數(shù)組中添加一個元素:
'urlManager' => array( 'urlFormat' => 'path', 'showScriptName' => false, // 添加這一行 'rules' => array( '<controller:w+>/<id:d+>'=>'<controller>/view', '<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action>', '<controller:w+>/<action:w+>'=>'<controller>/<action>', ), ),
7)完成!