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 None 將None改為 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] 

即可!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM