我今天對於尾部斜槓剛剛有同樣的問題。這是我怎麼會解決它在我的.htaccess文件(重要的是DirectorySlash和第一重寫規則):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
#Do not automatically add a slash for existing directories
DirectorySlash Off
#No trailing slash. This means that an URL entered with trailing/will change to an URL without trailing/
#The !-d rule can be included so that any URLs to existing directories are not changed
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule^%1 [L,R=301]
#Allow URL's which do not have the php file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule^%{REQUEST_FILENAME}.php [L]
#Redirect any file which does not exist to the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [R=301,L]
</IfModule>
所以,如果你鍵入URL作爲其example.com/index/會重定向到example.com /指數。如果你輸入example.com/chatroom(和chatroom.php存在),它仍然在URL中有example.com/chatroom,但你的PHP腳本收到example.com/chatroom.php
你將不得不添加的是這兩種情況下重定向index.php到/並刪除www
我已包括(我不知道你是否需要它)是最後一條規則,將任何不存在的文件重定向到index.php
來源
2015-07-14 03:22:16
Tho
謝謝你的鏈接,但我仍然很困惑如何做重定向。他們並沒有真正展示如何在同一時間做到這一切。 – Deshar
要重寫URL,應該在Web目錄的根文件夾中創建一個.htaccess文件。並且必須將以下代碼作爲您的要求。 選項+的FollowSymLinks RewriteEngine敘述 上 重寫規則^(。*)\。HTM $ $ 1.PHP [NC] 你檢查這些行。 您可以從任何網站下載.htaccess文件。 –