1. 程式人生 > 程式設計 >ThinkPHP6.0 重寫URL去掉Index.php的解決方法

ThinkPHP6.0 重寫URL去掉Index.php的解決方法

踩坑!

官網給的解決方案:解決重寫URL,省去index.php問題

可以通過URL重寫隱藏應用的入口檔案index.php,下面是相關伺服器的配置參考:

[ Apache ]
httpd.conf配置檔案中載入了mod_rewrite.so模組
AllowOverride None 將None改為 All

把下面的內容儲存為.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>

官網文件中給的 .htaccess 檔案內容如下

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

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

正確的寫法應該如下,官網給的最後一行配置錯誤

<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>

結尾

解決問題最好多看看官方給的文件,但有時候也不能全看官網文件,可以結合官網下面的討論區,借鑑各個大佬們的回答去解決問題