thinkphp5去除index.php的几大方式,根治各种不行?

在tp5中官方给出的去隐藏index.php方法如下:

隐藏的index.php

PS:这里说的入口文件指的是公共/ index.php文件,配置文件就在这个目录下

可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则。

Apache为例,在需要文件入口的同级添加.htaccess文件(官方默认自带了该文件),内容如下:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

如果用的phpstudy,规则如下:

<IfModule mod_rewrite.c> 
Options +FollowSymlinks -Multiviews 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] 
</IfModule>

如果index.php文件存放在public中,规则如下:

<IfModule mod_rewrite.c> 
Options +FollowSymlinks -Multiviews 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ public/index.php [L,E=PATH_INFO:$1] 
</IfModule>

接下来就可以使用下面的URL地址访问了

http://tp5.com/index/index/index
http://tp5.com/index/index/hello

如果使用你的apache版本使用上面的方式无法正常隐藏index.php,尝试可以使用下面的方式配置.htaccess文件:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

如果的英文Nginx环境的话教育,在可以Nginx.conf中添加:

location / { // …..省略部分代码
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        break;
    }
}

======================================================================================================================================================================================

[ Apache ]

  1. httpd.conf配置文件中加载了mod_rewrite.so模块
  2. AllowOverride NoneNone改为 All
  3. 把下面的内容保存为.htaccess文件放到应用入口文件的同级目录下
    <IfModule mod_rewrite.c>
      Options +FollowSymlinks -Multiviews
      RewriteEngine On
     
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
    </IfModule>
    

[ Nginx ]

在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:

location / { // …..省略部分代码
   if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
    }
}

  

本人本地环境如下:phpstudy2018。官方给出的方法在一些集中的PHP环境中应该是可用的(本人没测过)。

今天本人配了一thinkadmin,折腾许久去不掉index.php。

改进方法有如下几种:

1、在index.php后面加个问号。如果从url地扯上理解,应该是问号后面算是参数(tp实现MVC原理就根据这个了),我写过dede二开,也是传不同参数调用不同方法。

RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]

2、用tp phpinfo兼容模式,即加了s

RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]

3、加上PHPINFO参数

RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

————————————————

版权声明:本文为CSDN博主「陆平平」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u011383596/article/details/80663471

另一博主文章:

public文件夹下,有个.htacess文件,没有则新建一个, 如果已有这个文件,原文件内容如下:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

如果此时还是报错 : “No input file specified.”;

那么就重写规则把最后一行

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

改为

RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]

即可!