Yii url重写
2013-03-14 17:02
190 查看
在弄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了。
http://hi.baidu.com/qteqpid_pku/blog/item/2909cf6dd610e8e742169494.html
修改前: 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了。
http://hi.baidu.com/qteqpid_pku/blog/item/2909cf6dd610e8e742169494.html
相关文章推荐
- yii url重写去掉index.php
- ubunut+nginx + yii + url重写(url rewrite)+mac+apache
- Yii Url重写
- Yii url重写
- Yii url重写(还没有试过)
- Yii url重写自定义url显示样式
- Yii url重写规则 添加html后缀
- Yii的url重写
- PHP Yii框架中使用smarty模板
- [YII2] 自定义组件,在YII框架中正常使用
- Yii: 扩展CGridView增加导出CSV功能
- YII 框架学习[1] —— 引入Yii框架
- Yii AR Model 查询
- yii2权限控制rbac之rule详细讲解
- Yii中使用twig
- yii小结
- windows 下YII框架初试
- Yii控制器中操作视图js的方法
- Yii 快速入门
- Yii框架开发问题总结