Heim  >  Artikel  >  Backend-Entwicklung  >  thinkphp3.2.X pathinfo 在lnmp下的终极解决方案

thinkphp3.2.X pathinfo 在lnmp下的终极解决方案

巴扎黑
巴扎黑Original
2016-11-09 14:25:251670Durchsuche

最近用了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;
        }

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

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

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'],'/'));
        }

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn