开发者

mod_rewrite: remove trailing slash (only one!) [duplicate]

开发者 https://www.devze.com 2023-01-04 08:28 出处:网络
This question already has answers here: mod_rewrite: remove trailing slash (only one!) (5 answers) Closed 4 years ago.
This question already has answers here: mod_rewrite: remove trailing slash (only one!) (5 answers) Closed 4 years ago.

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/b开发者_如何学编程ar/ 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.


Try this pattern please

^(.+[^/])/$ instead of ^(.+)/$

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号