首页 > CodeIgniter, nginx > codeigniter的url去掉index.php

codeigniter的url去掉index.php

版权声明:原创作品,转载请务必以超链接形式标明文章 原始出处 、作者和本声明,否则将追究法律责任。

最近使用codeigniter做了个网站,为了保持url的美观,也为了安全起见,决定去掉url中的index.php。

起初google了半天也没找到正确的写法,最后还是自己边看手册边摸索地写了出来,自己学到了很多,同时也分享给大家。

CI中的路径配置是
$config['uri_protocol'] = ‘AUTO’;
其它配置没有测试,请自己尝试。

以下是nginx.conf的配置:

server {
  listen       80;
  server_name  abc.com;
  root   /www/abc;
  location / {
    index  index.php index.html;
    if (!-e $request_filename) {
       rewrite  ^/(.*)$  /index.php/$1  last; // 此处last不可换用break
    }
  }
# location ~ ^/index\.php(/.*)+$ { // 不可在此处
#   return 404;                // 限制访问index.php
# }                      // 否则整个网站将不能访问
   location ^~ /(application|system) {
     deny  all;
     break;
   }
   location ~ /\. {
     deny  all;
   }
   location ~ \.php($|/) {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      set            $script     $uri;
      set            $path_info  "";
      if ($request_uri ~ "^/index\.php") { // 此处是$request_uri,不是$uri
         return 404;
      }
      if ($uri ~ "^(.+?\.php)(/.*)$") {  // 此处是$uri,不是$request_uri
         set  $script     $1;
         set  $path_info  $2;
      }
      include        fastcgi_params;
      fastcgi_param  PATH_INFO        $path_info;
      fastcgi_param  SCRIPT_FILENAME  /www/abc/index.php;
      fastcgi_param  SCRIPT_NAME      $script;
   }
}
分类: CodeIgniter, nginx 标签: , ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.