Yii url重写
2011-12-28 16:25
232 查看
在弄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重写自定义url显示样式
- Yii url重写
- Yii url重写规则 添加html后缀
- Yii的url重写
- yii url重写去掉index.php
- ubunut+nginx + yii + url重写(url rewrite)+mac+apache
- Yii Url重写
- Yii url重写(还没有试过)
- YII内置验证规则
- Interface yii\base\Configurable
- Yii里获取当前module controller action的id
- Class yii\base\ExitException
- Yii CJuiDatePicker显示一个日期选择器
- php视频教程实战之Yii2.0打造完整电商平台
- Yii tips 2
- thinkphp url重写规则及wamp配置
- Yii CDbCriteria 常用方法
- yii2.0缓存篇之片段缓存
- Yii 图片FTP批量上传 并生成缩略图
- URL重写技术