非 www 到 www 重定向不会删除尾部反斜杠

发布于 2025-01-05 22:26:12 字数 986 浏览 2 评论 0原文

我在 Apache 重定向方面遇到了一些问题。 虽然以下规则适用于网站上的任何页面,但 mydomain.com 将被重定向到 mydomain.com//,这会忽略尾部斜杠删除规则。

使用诸如此类的多个规则是否有效,或者我应该尝试将它们组合起来或以某种方式将它们链接在一起以避免单个网址的多个重定向?

谢谢

#Turn on options for url rewriting
         Options +FollowSymlinks
         RewriteEngine on

 #lovercase all urls
         RewriteMap  lc int:tolower
         RewriteCond %{REQUEST_URI} [A-Z]
         RewriteCond %{REQUEST_URI} ^/fonts/.*
         RewriteCond %{REQUEST_URI} ^/css/.*
         RewriteCond %{REQUEST_URI} ^/js/.*
         RewriteRule (.*) ${lc:$1} [R=301,L]

 #redirect all requests made to http:// to http://www.
         RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
         RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

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

I'm having a bit of problem with Apache redirect.
While bellow rules work for any page on site, mydomain.com will get redirected to mydomain.com//, which ignores trailing slash removal rule.

Also is it efficient to use multiple rules such as this or should I try to combine them or chain them somehow together in order to avoid multiple redirects for single url?

Thanks

#Turn on options for url rewriting
         Options +FollowSymlinks
         RewriteEngine on

 #lovercase all urls
         RewriteMap  lc int:tolower
         RewriteCond %{REQUEST_URI} [A-Z]
         RewriteCond %{REQUEST_URI} ^/fonts/.*
         RewriteCond %{REQUEST_URI} ^/css/.*
         RewriteCond %{REQUEST_URI} ^/js/.*
         RewriteRule (.*) ${lc:$1} [R=301,L]

 #redirect all requests made to http:// to http://www.
         RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
         RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

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

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

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

发布评论

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

评论(1

起风了 2025-01-12 22:26:12

mydomain.com 被重定向到 www.mydomain.com// 的原因是因为您的重写规则目标中有一个额外的“/”:

RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
                                          ^----here

当您在您的服务器/虚拟主机配置中,前导斜杠不会被删除,以便获得匹配并用作反向引用,因此 mydomain.com/ ,它匹配 ^( .*)$ 目标变为http://www.mydomain.com//。因此,您可以删除目标中的斜杠或在正则表达式中添加斜杠:

RewriteRule ^(.*)$ http://www.mydomain.com$1 [R=301,L]
or
RewriteRule ^/(.*)$ http://www.mydomain.com/$1 [R=301,L]

您拥有的另一条规则:

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

没问题。它们用于在它们之间有某些内容时删除尾部斜杠,例如 /something/,因为 (.+)。无论如何,它都不会匹配 //,因为它本质上会变成 /。您只需要防止重定向到 http://www.mydomain.com//

The reason the mydomain.com gets redirected to www.mydomain.com// is because you have an extra "/" in your rewrite rule target:

RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
                                          ^----here

When you have rules in your server/vhost config, the leading slash isn't removed so that gets match and used as a backreference, so mydomain.com is / which matches ^(.*)$ and the target becomes http://www.mydomain.com//. So you can either remove the slash in the target or add one to the regex:

RewriteRule ^(.*)$ http://www.mydomain.com$1 [R=301,L]
or
RewriteRule ^/(.*)$ http://www.mydomain.com/$1 [R=301,L]

Your other rule you have:

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

are fine. They are for removing trailing slashes when there is something between them, e.g. /something/, because of the (.+). It wouldn't match // anyways because that inherently gets turned into just /. You just need to prevent redirecting to http://www.mydomain.com//

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