非 www 到 www 重定向不会删除尾部反斜杠
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mydomain.com
被重定向到www.mydomain.com//
的原因是因为您的重写规则目标中有一个额外的“/”:当您在您的服务器/虚拟主机配置中,前导斜杠不会被删除,以便获得匹配并用作反向引用,因此
mydomain.com
是/
,它匹配^( .*)$
目标变为http://www.mydomain.com//
。因此,您可以删除目标中的斜杠或在正则表达式中添加斜杠:您拥有的另一条规则:
没问题。它们用于在它们之间有某些内容时删除尾部斜杠,例如
/something/
,因为(.+)
。无论如何,它都不会匹配//
,因为它本质上会变成/
。您只需要防止重定向到http://www.mydomain.com//
The reason the
mydomain.com
gets redirected towww.mydomain.com//
is because you have an extra "/" in your rewrite rule target: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 becomeshttp://www.mydomain.com//
. So you can either remove the slash in the target or add one to the regex:Your other rule you have:
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 tohttp://www.mydomain.com//