rewrite: remove trailing slash (only one!)

I use mod_rewrite/.htaccess for pretty URLs.

I'm using this condition/rule to eliminate trailing slashes (or rather: rewrite to the non-trailing-slash-URL, by a 301 redirect; I'm doing this to avoid duplicate content and because I like URLs with no trailing slashes better):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^.localhost$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

Working well so far. Only drawback:
it also forwards "multiple-trailing-slash"-URLs to non-trailing-slash-URLs.

Example:
http://example.tld/foo/bar////// forwards to http://example.tld/foo/bar
while I only want http://example.tld/foo/bar/ to forward to http://example.tld/foo/bar .

So, is it possible to only eliminate trailing slashes if it's actually just one trailing slash?

Sorry if this is a somewhat annoying or weird question!

Thanks.


the following rule will match any URL ending in a slash and remove all slashes from the end of it:

RewriteRule ^(.*)/+$ $1 [R=301,L]

Note: The currently accepted answer only works for http not https but this one works for both.


change the rewrite rule to:

RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]

in English: match the start of the string, one or more anything, NOT a slash, a slash, the end.


^(.+[^/])/$

即前角色不能是斜线。

链接地址: http://www.djcxy.com/p/36534.html

上一篇: 重写规则以隐藏文件夹,如果不使用斜线,则无法正常工作

下一篇: 重写:删除尾部斜线(只有一个!)