本文介绍了mod_rewrite:删除尾部斜杠(只有一个!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于漂亮的 URL,我使用 mod_rewrite/.htaccess.

I use mod_rewrite/.htaccess for pretty URLs.

我正在使用此条件/规则来消除尾部斜杠(或者更确切地说:通过 301 重定向重写为非尾部斜杠 URL;我这样做是为了避免重复内容,因为我喜欢带有没有尾随斜杠更好):

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]

到目前为止运行良好.唯一的缺点:
它还转发multiple-trailing-slash"-URLsnon-trailing-slash-URLs.

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

示例:
http://example.tld/foo/bar////// 转发到 http://example.tld/foo/bar
而我只希望 http://example.tld/foo/bar/ 转发到 http://example.tld/foo/bar.

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!

谢谢.

推荐答案

以下规则将匹配任何以斜杠结尾的 URL,并删除其末尾的所有斜杠:

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]

注意:当前接受的答案仅适用于 http 而不是 https,但此答案适用于两者.

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

这篇关于mod_rewrite:删除尾部斜杠(只有一个!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:50