2016-07-05 177 views
1

我知道這個標題有一個巴吉爾數量的問題,我已經嘗試過。我想要做的是重定向在url結尾處刪除尾部斜槓

localhost/site/tours/picnics/ 

localhost/site/tours/picnics 

但每次我試圖代碼,(這一個實例)

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] 
RewriteRule ^(.+?)/$ /$1 [R=301,L] 

重定向我localhost/picnics爲一些原因。我不明白爲什麼會發生。我甚至試過RewriteBase /site/但它沒有任何區別。有人可以指出這個代碼中的問題嗎?

編輯:這裏是我的完整htaccess文件

RewriteEngine On 
#RewriteBase /site/ 
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L] 
#RewriteRule ^tours/? tour-listing.php [NC,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] 
RewriteRule ^(.+?)/$ /$1 [R=301,L] 

RewriteRule ^tours/?(.*)$ tour-listing.php?cat=$1 [NC,L] 
RewriteRule ^tour/([0-9a-zA-Z]+)$ tour.php?id=$1 [NC,L] 
RewriteRule ^hotel/([0-9a-zA-Z]+)$ hotel-details.php?id=$1 [NC,L] 
+0

在localhost/site/ – VeeK

回答

1

您的規則改成這樣:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s] 
RewriteRule /$ /%1 [R=301,L,NE] 

$1相對值捕捉到只,但%1正在從%{THE_REQUEST}捕捉你的當前目錄有原始和完整的請求URL。

+0

請務必保留此規則在'RewriteBase'行的下方 – anubhava

+0

它可以工作,但它會在'localhost'後面添加一個/,每次我到鏈接localhost/site/tours/picnic /'它變成'localhost // site/tours/picnic',再次進入'localhost /// site/tours/picnic /',再次'localhost //// site/tours/picnic /' – VeeK

+1

很棒。有用。非常感謝 – VeeK