Yii的URL管理精要

通常在yii框架的Url中如下: http://yourdomain.com/index.php?r=account/login

1. Friendly URL(美化URL)
修改config/main.php,增加一个component

 'urlManager'=>array(  
   'urlFormat'=>'path',  
  ), 

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

   
   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  
  

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

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

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

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

29 + = 35