1. 程式人生 > >Nginx下使Thinkphp URL模式支援PATHINFO和REWRITE

Nginx下使Thinkphp URL模式支援PATHINFO和REWRITE

1.支援PATHINFO

找到location ~ \.php {   ,把$去掉,並在裡面加上這兩句

fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加
fastcgi_param PATH_INFO $fastcgi_path_info;    #增加

location ~ \.php { #去掉$
            root       html
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param
SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(.*)$; #新增 fastcgi_param PATH_INFO $fastcgi_path_info; #新增 }

2.支援REWRITE去掉index.php

找到location / {  在裡面加上

if (!-e $request_filename
) { rewrite ^/(.*)$ /index.php?s=$1 last; break; }

location / {
            root       html 
            index  index.html index.htm index.php;

            if (!-e $request_filename) {                 #新增
                   rewrite  ^/(.*)$ /index.php?s=$1  last; #新增
                   break
; #新增 } #新增 }

如果thinkphp不是部署在網站根目錄下,即需要把

rewrite  ^/(.*)$ /index.php?s=$1  last;
改成
rewrite  ^/子目錄/(.*)$ /子目錄/index.php?s=$1  last;

如果根目錄下有多個專案的話,可以寫多個rewrite:

 if (!-e $request_filename) {
      rewrite  ^/專案1/(.*)$ /專案1/index.php?s=$1  last;
      rewrite  ^/專案2/(.*)$ /專案2/index.php?s=$1  last;
      rewrite  ^/專案3/(.*)$ /專案3/index.php?s=$1  last;
      break;
 }