nginx配合codeIgniter有点麻烦,没有apache和lighttpd那么简单。
在apache和lighttpd里面,判断 /index.php/sth 时,如果 /index.php是一个文件,就不会当做是文件路径了
我们可以这样简单的把某些路径rewrite,达到一样的效果:
1 保持地址格式是 /index.php/control/model
2 不产生404错误或者是no input的情况
我们还需要避免类似以下地址异常:
/book/?test 404 查询字符会查找test的control
/book/&test 错误 The URI you submitted has disallowed characters.
总体而言,是查询字符的问题,我们可以有两种方式处理:
方式一:在codeIgniter处理之前避免
我们定义系统使用REQUEST_URI来处理mvc参数,不修改系统的文件,我们修改自定义的文件
文件 application/config/config.php
指定为REQUEST_URI方式处理,不是用auto,auto方式会判断查询字符
文件 index.php
我们可以直接砍掉查询字符,保证参数不受影响
方式二:发生错误之后调整
发生404错误时,我们自动跳转到相应的地址,比如去掉后面 ?xxxx 和 &xxxx
文件 application/errors/error_404.php
在 error_general.php 里也可以这样加一样的代码。
参考资料:
1 不错的nginx配置方式,本文有参考: CodeIgniter Nginx Rewrite Rule
2 CodeIgniter URLs & Nginx
3 lighttpd rewrite rule && apache mod_rewrite
原创内容如转载请注明:来自 阿权的书房
$HTTP["host"] == "www.aslibra.com" {
server.document-root = "/path/to/www.aslibra.com/"
url.rewrite = (
"^/static/.*$" => "$0",
"^/(.*)$" => "index.php/$1"
)
fastcgi.server = ( ".php" =>
( "weibo"=>( "host" => "127.0.0.1","port" => 9000) )
)
}
server.document-root = "/path/to/www.aslibra.com/"
url.rewrite = (
"^/static/.*$" => "$0",
"^/(.*)$" => "index.php/$1"
)
fastcgi.server = ( ".php" =>
( "weibo"=>( "host" => "127.0.0.1","port" => 9000) )
)
}
在apache和lighttpd里面,判断 /index.php/sth 时,如果 /index.php是一个文件,就不会当做是文件路径了
我们可以这样简单的把某些路径rewrite,达到一样的效果:
1 保持地址格式是 /index.php/control/model
2 不产生404错误或者是no input的情况
server {
index index.php index.htm;
server_name www.aslibra.com;
root /Data/webapps/www.aslibra.com;
location /book/ {
if (!-e $request_filename) {
rewrite ^/book/(.*)$ /book/index.php/$1 last;
}
}
location /book/index.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Data/webapps/www.aslibra.com/book/index.php;
}
}
index index.php index.htm;
server_name www.aslibra.com;
root /Data/webapps/www.aslibra.com;
location /book/ {
if (!-e $request_filename) {
rewrite ^/book/(.*)$ /book/index.php/$1 last;
}
}
location /book/index.php {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Data/webapps/www.aslibra.com/book/index.php;
}
}
我们还需要避免类似以下地址异常:
/book/?test 404 查询字符会查找test的control
/book/&test 错误 The URI you submitted has disallowed characters.
总体而言,是查询字符的问题,我们可以有两种方式处理:
方式一:在codeIgniter处理之前避免
我们定义系统使用REQUEST_URI来处理mvc参数,不修改系统的文件,我们修改自定义的文件
文件 application/config/config.php
指定为REQUEST_URI方式处理,不是用auto,auto方式会判断查询字符
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = "REQUEST_URI";
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = "REQUEST_URI";
文件 index.php
我们可以直接砍掉查询字符,保证参数不受影响
//处理查询字符
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
$_SERVER['REQUEST_URI'] = $url[0];
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
$_SERVER['REQUEST_URI'] = $url[0];
/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;
方式二:发生错误之后调整
发生404错误时,我们自动跳转到相应的地址,比如去掉后面 ?xxxx 和 &xxxx
文件 application/errors/error_404.php
<?php
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
if($url[0] != $_SERVER['REQUEST_URI']){
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url[0]\">";
}
?>
</head>
$url = explode('?', $_SERVER['REQUEST_URI']);
$url = explode('&', $url[0]);
if($url[0] != $_SERVER['REQUEST_URI']){
echo "<meta http-equiv=\"refresh\" content=\"0;url=$url[0]\">";
}
?>
</head>
在 error_general.php 里也可以这样加一样的代码。
参考资料:
1 不错的nginx配置方式,本文有参考: CodeIgniter Nginx Rewrite Rule
2 CodeIgniter URLs & Nginx
3 lighttpd rewrite rule && apache mod_rewrite
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
《我们台湾这些年》以及独立电影
天意淘回来的玩偶_1
