2016-07-05 124 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