mod_rewrite:删除尾部斜杠(只有一个!)

发布于 2024-09-06 09:14:19 字数 667 浏览 1 评论 0原文

我使用 mod_rewrite/.htaccess 来获得漂亮的 URL。

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

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

到目前为止工作良好。唯一的缺点:
它还将“多个尾随斜杠”URL 转发到“非尾随斜杠 URL”。

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

那么,如果实际上只有一个尾部斜杠,是否可以只消除尾部斜杠?

抱歉,如果这是一个有点烦人或奇怪的问题!

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-09-13 09:14:19

请尝试这个模式

^(.+[^/])/$ 而不是 ^(.+)/$

Try this pattern please

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文