Yii地址美化(nginx环境)

时间:2023-03-09 08:26:51
Yii地址美化(nginx环境)

通过urlmanager实现yii地址美化,需配合服务器中的rewrite配置

   

1、在'components'中加入
  
'urlManager'=>array(
      
'urlFormat'=>'path',                
//使用pathinfo模式,不需要?r=
      
'showScriptName'=>false,            
//隐藏index.php
      
'rules'=>array(                     
//这里面写规则,下文再做解释
          
'pattern1'=>'route1',
          
'pattern2'=>'route2',
          
'pattern3'=>'route3',
      
),
   ),

2、在nginx中配置
找到相应站点的配置文件,nginx.conf,但如果你是lnmp,它会自动调用conf里的站点配置文件,你可在/usr/local/nginx/conf/vhost
中找到。
添加以下规则:(fastcgi_pass视具体服务器配置而定,不必更改。若规则已存在,加入红色两行)
    location ~
.*\.(php|php5)?$ {
       
fastcgi_pass  unix:/tmp/php-cgi.sock;
       
fastcgi_index index.php;
       
include fcgi.conf;
       
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
       
fastcgi_param PATH_INFO $fastcgi_script_name;
    }

若你在1中配置urlmanager为不显示index.php,则还需要添加如下规则:
   
location / {
       
if (!-e $request_filename){
           
rewrite ^/(.*) /index.php last;
       
}
   
}
    # if(!-e
$request_filename)的意义是先确定访问地址是否是真实存在的文件,否不是,则 rewrite 至
index.php

3、在apache中配置

在根目录下建立 .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