您的位置:首页 > 编程语言 > PHP开发

Yii的重写规则与URL的管理

2014-06-09 18:25 357 查看
通常在yii框架的Url中如下: http://yourdomain.com/index.php?r=account/login
1. Friendly URL(美化URL)

主要实现这样的url : http://yourdomain.com/site/contact.html
修改config/main.php,增加一个component
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false, //去除index.php
'urlSuffix'=>'.html', //加上.html
'rules'=>array(
'pattern1'=>'route1',
'pattern2'=>'route2',
'pattern3'=>'route3',
),
),

2. 使用URL重写,去掉index.php

在你的app根目录下创建.htaccess内容如下:
<IfModule mod_rewrite.c>

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

</IfModule>


当然前提是要在httpd.conf中打开apache的rewrite模块

3. Yii创建URL时去掉index.php

再次修改config/main.php,在刚才UrlManager组件增加属性showScriptName,值为false.


YII模块绑定二级域名方法

 

在配置文件设置
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false, //注意false不要用引号括上
'urlSuffix' => '.html',
'rules' => array(
'http://blog.zeeeda.com'=>array('/blog', 'urlSuffix'=>'', 'caseSensitive'=>false),
'http://blog.zeeeda.com/comment-<id:\w+>'=>array('/blog/comment/', 'urlSuffix'=>'.html', 'caseSensitive'=>false),//blog 为一个模块 ,如果在blog模块下还存在第二个控制器(这里以comment为例),则需要多写一个规则
),
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: