在弄yii的url重写,希望能把url改成更好记的形式,同时去掉index.php的部分。转化前后的对比: 修改前: http://localhost/index.php?r=site/page?view=about 修改后: http://localhost/site/page?view=about 查了些资料完成以上的效果。我到环境是: apache2(httpd-2.2.21), PHP5.3.8, Yii 1.1.8 STEP 1 首先先确保apache2有rewrite模块,具体可通过phpinfo()看到apache的模块。 我在这步折腾了不少时间,不管怎么编译都出不来,最后是通过如下完成到: cd /home/qteqpid/Software/httpd-2.2.3/modules/mappers/ /usr/local/apache2/bin/apxs -c mod_rewrite.c /usr/local/apache2/bin/apxs -i -a -n mod_rewrite mod_rewrite.la 如果没出错,在/usr/local/apache2/modules/ 中就会有mod_rewrite.so了 注意,然后只能在httpd.conf文件里添加以下一行,, LoadModule rewrite_module modules/mod_rewrite.so 重启apache即可。 STEP 2 然后修改protected/config/main.php,修改如下: 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, // 这一步是将代码里链接的index.php隐藏掉。 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), 这一步之后,在yii里的所有链接将不会看到index.php,但还应该到apache在收到如此url请求时懂得加上index.php,这就需要url重写了。 STEP 3 有了STEP1的前提,就可以在yii项目的根目录(和index.php同级目录)下添加.htaccess目录,在里面写上: <IfModule rewrite_module> Options +FollowSymLinks IndexIgnore */* RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php </IfModule> 就ok了。