2012-02-12 89 views
1

我使用.htaccess mod_rewrite更好的URL。如何解決與.htaccess mod_rewrite和尾部斜槓問題

這裏是我的.htaccess文件的一部分,輪流到/artist.php?id=123456/artist/123456/artists.php?culture=arabic/artists/arabic,並刪除.php爲:

RewriteRule ^artist/([0-9]+) artist.php?id=$1 
RewriteRule ^artists/(.+) artists.php?culture=$1 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

它的工作原理,但如果我把一個尾隨在年底削減//artist/arabic/它會引發404錯誤。我怎樣才能解決這個問題?簡單地刪除尾部的斜線可能會工作,但我不知道如何做到這一點。我只是不希望用戶不小心添加/或其他東西,然後感到困惑,認爲該頁面不存在。

在此先感謝!

回答

3

你只需要添加一個可選的尾部的斜槓到第一個規則與/?

# Optional trailing slash... 
RewriteRule ^artist/([0-9]+)/? artist.php?id=$1 
#--------------------------^^^^ 
RewriteRule ^artists/(.+) artists.php?culture=$1 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 
+0

謝謝,它的工作原理。而令人驚訝的是,它適用於另一個目錄的.htaccess文件(它添加了可選項/實際上並沒有將規則放入它中......是因爲它將在第一條規則中應該如何工作?) – Nathan 2012-02-12 04:01:00

+0

哦,那是因爲[L]國旗被停止,導致它落到下一場比賽。你通常不會那麼想,而且我無意中將它留在了我的答案中。 – 2012-02-12 04:08:13