天天看點

詳解如何在url中去掉index.php

THINKPHP官方論壇和網絡上很多NGINX配置教程不是太完善的。我一直比較喜歡使用lnmp.org配置伺服器環境。是以本文将介紹 LNMP 配置NGINX 支援THINKPHP PATHINFO模式 。

thinkphp的url通路:http://serverName/index.php(或者其它應用入口檔案)/子產品/控制器/操作/[參數名/參數值...],這個需要支援pathinfo,Apache預設支援,而Nginx不支援。

1,修改PHP.INI 支援 PHPINOF

PHP配置檔案:/usr/local/php/etc/php.ini

更改php.ini

找到:cgi.fix_pathinfo=0

更改為:cgi.fix_pathinfo=1

詳解如何在url中去掉index.php
詳解如何在url中去掉index.php

2.修改nginx對應配置檔案

LNMP上各個版本pathinfo各個版本的設定基本一樣:

lnmp v1.1上,修改對應虛拟主機的配置檔案

去掉#include pathinfo.conf前面的#,把try_files $uri =404; 前面加上# 注釋掉。

1.2, 1.3上,修改對應虛拟主機的配置檔案

将include enable-php.conf;替換為include enable-php-pathinfo.conf;

修改pathinfo需要重新開機nginx生效。

去掉 index.php

location / {

if (!-e $request_filename) {

rewrite ^/(.*)$ /index.php/$1 last;

}

}

linux中 /usr/local/nginx/conf/配置檔案

詳解如何在url中去掉index.php

windows中

詳解如何在url中去掉index.php

注意: root配置要定位到yyyy的位置,不一定是localhost

"D:/xxxx/localhost/yyyy";

3.重新開機lnmp即可。

lnmp restart

4.設定url_mode =2

// -----------URL僞靜态字尾設定----------
'URL_MODEL'=>'2',  // 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE  模式); 3 (相容模式)  預設為PATHINFO 模式           

複制

注:有些文章提到nginx不支援pathinfo,需要在config檔案中設定。

我沒遇到過! 下面是别人的解決方法,僅供參考。

location ~ \.php {    #去掉$
         root          H:/PHPServer/WWW;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加這一句
         fastcgi_param PATH_INFO $fastcgi_path_info;    #增加這一句
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include        fastcgi_params;
    }             

複制

還有人是這麼配置的:

server {
listen 80;
root *****************/public_html;
server_name **************.com;
index index.html index.php index.htm;
error_page 400 /errpage/400.html;
error_page 403 /errpage/403.html;
error_page 404 /errpage/404.html;
error_page 503 /errpage/503.html;
location ~ \.php(.*)$ {
fastcgi_pass unix:/tmp/php-70-cgi.sock;
fastcgi_index index.php;
fastcgi_param script_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $2;
include fcgi.conf;
########################################################################
pathinfo配置開始
########################################################################
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param script_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
########################################################################
pathinfo配置結束
########################################################################
}
           

複制