thinkphp3.2.X pathinfo ultimate solution under lnmp

巴扎黑
Release: 2016-11-09 14:25:25
Original
1709 people have browsed it

最近用了lnmp一键安装包1.2版本,然后搭建的ThinkPHP程序无论如何都不能用pathinfo模式。网上找了很多解决方案,最后发现了问题所在。 
1、lnmp一键安装包的php.ini里面cgi.fix_pathinfo=0。而php正常安装的话这个值是默认为1的。如果你用lnmp一键安装包必须要修改成1才行,当然你也可以修改Thinkphp源码。 
2、修改你的配置文件,注释掉try_files $uri =404;打开include pathinfo.conf;然后把URL_MODEL=1就能支持pathinfo了。 

 location ~ [^/]\.php(/|$)
        {
            # comment try_files $uri =404; to enable pathinfo
            #try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            include pathinfo.conf;
        }
Copy after login

3.如果你觉得cgi.fix_pathinfo=1不安全可以用REWRITE模式
在虚拟主机root那一行下面加上

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

4.导致pathinfo不能用的原因是cgi.fix_pathinfo=0的时候$_SERVER['PHP_SELF']是获取不到index.php的。
而Thinkphp的源码

if(IS_CGI) {
            //CGI/FASTCGI模式下
            $_temp  = explode('.php',$_SERVER['PHP_SELF']);
            define('_PHP_FILE_',    rtrim(str_replace($_SERVER['HTTP_HOST'],'',$_temp[0].'.php'),'/'));
        }else {
            define('_PHP_FILE_',    rtrim($_SERVER['SCRIPT_NAME'],'/'));
        }
Copy after login

由于$_SERVER['PHP_SELF']获取不到index.php所以$_temp是空,导致_PHP_FILE_为.php,U函数生成的地址也是.php这样无法访问的地址。如果你不想修改PHP的配置就只能修改这里的源码。把上面的if里面修改成和else下面一样的就可以了。 

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!