Yii隐藏index.php,主要用到web服务器的rewrite和pathinfo功能。rewrite是将无法找到的地址rewrite到index.php
后面。不管什么web服务器首先要开启rewrite功能。apache要开启rewrite_module,即:
去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号
确保<Directory “…”></Directory>中有“AllowOverride All”。Nginx一般编译时候都会带上rewrite模块。
pathinfo是由于框架获取路径信息使用到的$_SERVER数组中pathinfo信息需要web服务器提供。但
Nginx默认不支持pathinfo。下面记下两个YII隐藏index.php的方法,感觉和CI框架差不多。
Nginx下的配置:
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;//主要是这行
access_log off;
}
location / {
if (!-e $request_filename){
rewrite ^/(.*) /index.php last; //将请求出错的路径rewrite到index.php上
}
}
Apache下的配置:
在与index.php文件同级目录下添加文件“.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
最后在YII的配置文件main.php中修改urlManager类似下面:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'post/<id:\d+>/<title:.*?>'=>'post/view',//此处重要:把post/id/title映射为:post/view/id/title
'posts/<tag:.*?>'=>'post/index',//此处重要:把通过“posts/tag”访问的链接变成post/index/tag
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
转自http://denghai260.blog.163.com/blog/static/72686409201281901727789/