YII的重写规则与URL的管理

49 篇文章 0 订阅
42 篇文章 0 订阅

本人小菜鸟一只,为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,服务器)等一系列的知识,小菜鸟创建了一个群。希望光临本博客的人可以进来交流。寻求共同发展。搭建平台。本人博客也有许多的技术文档,希望可以为你提供一些帮助。

QQ群: 191848169 QQ:450225664



通常在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

  1. 'urlManager'=>array(  
  2.             'urlFormat'=>'path',  
  3.             'showScriptName' => false, //去除index.php  
  4.             'urlSuffix'=>'.html', //加上.html  
  5.             'rules'=>array(  
  6.                 'pattern1'=>'route1',  
  7.                 'pattern2'=>'route2',  
  8.                 'pattern3'=>'route3',  
  9.             ),  
  10.         ),  



2. 使用URL重写,去掉index.php
在你的app根目录下创建.htaccess内容如下:

 

  1. <IfModule mod_rewrite.c>   
  2.   
  3.   
  4. Options +FollowSymLinks  
  5. IndexIgnore */*  
  6. RewriteEngine on  
  7.   
  8. # if a directory or a file exists, use it directly  
  9. RewriteCond %{REQUEST_FILENAME} !-f  
  10. RewriteCond %{REQUEST_FILENAME} !-d  
  11.   
  12. # otherwise forward it to index.php  
  13. RewriteRule . index.php  
  14.   
  15.   
  16. </IfModule>  


 



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


3. Yii创建URL时去掉index.php
再次修改config/main.php,在刚才UrlManager组件增加属性showScriptName,值为false.

  1. 'urlManager'=>array(    'urlFormat'=>'path',    'showScriptName' => false,   ),  



 

YII模块绑定二级域名方法

 

在配置文件设置

  1. 'urlManager' => array(  
  2. 'urlFormat' => 'path',  
  3. 'showScriptName' => false, //注意false不要用引号括上  
  4. 'urlSuffix' => '.html',  
  5. 'rules' => array(  
  6. 'http://blog.zeeeda.com'=>array('/blog', 'urlSuffix'=>'', 'caseSensitive'=>false),  
  7. 'http://blog.zeeeda.com/comment-<id:\w+>'=>array('/blog/comment/', 'urlSuffix'=>'.html', 'caseSensitive'=>false),//blog 为一个模块 ,如果在blog模块下还存在第二个控制器(这里以comment为例),则需要多写一个规则  
  8. ), 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值