在 RewriteRule 中匹配 HTTP_HOST,而不是使用 RewriteCond
我需要制作一堆 301 重定向,其中包含来自同一 .htaccess
文件中的不同域。我可以为每个规则编写一个 RewriteCond,如下所示,它工作得很好:
RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule old-url-1a.htm /new-url-1a [L,R=301]
RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule old-url-1b.htm /new-url-1b [L,R=301]
RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule old-url-2a.htm /new-url-2a [L,R=301]
RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule old-url-2b.htm /new-url-2b [L,R=301]
但是使用这种方法,我需要为每个 URL 编写 2 行代码,这会变得非常冗长且重复(相同的 RewriteCond 会重复很多次)。 有什么方法可以匹配 RewriteRule 指令本身中的 HTTP_HOST 吗?沿着这些思路(概念上):
RewriteRule www.domain1.com/old-url-1a.htm /new-url-1a [L,R=301]
RewriteRule www.domain1.com/old-url-1b.htm /new-url-1b [L,R=301]
RewriteRule www.domain2.com/old-url-2a.htm /new-url-2a [L,R=301]
RewriteRule www.domain2.com/old-url-2b.htm /new-url-2b [L,R=301]
谢谢。
I have a bunch of 301 redirects I need to make which contain varying domains all from within the same .htaccess
file. I could write a RewriteCond for each rule as below and it works fine:
RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule old-url-1a.htm /new-url-1a [L,R=301]
RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule old-url-1b.htm /new-url-1b [L,R=301]
RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule old-url-2a.htm /new-url-2a [L,R=301]
RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule old-url-2b.htm /new-url-2b [L,R=301]
But using this method I need to have 2 lines of code for every single URL which gets very lengthy and repetitive (the same RewriteCond is repeated many times).
Is there any way I can match the HTTP_HOST from within the RewriteRule directive itself? Something along these lines (conceptually):
RewriteRule www.domain1.com/old-url-1a.htm /new-url-1a [L,R=301]
RewriteRule www.domain1.com/old-url-1b.htm /new-url-1b [L,R=301]
RewriteRule www.domain2.com/old-url-2a.htm /new-url-2a [L,R=301]
RewriteRule www.domain2.com/old-url-2b.htm /new-url-2b [L,R=301]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apache 文档中关于 RewriteRule 的描述如下:
但是,如果您能够将规则放入 apache-config 中,则还有另一种简写方法:您可以使用 RewriteMap。
host-redirects.map 看起来像这样:
The Apache Documentation says about RewriteRule:
But if you are able to put the rules inside your apache-config, there is another shorthand way: You could use a RewriteMap.
The host-redirects.map would look like this:
BTW 映射可能是调试“真正匹配的内容”的唯一方法。
您可以将 RewriteLogLevel 设置为 9,日志不会显示使用
RewriteCond
解析的内容。相反,它表示是否“匹配”。 不是很有用。
使用映射(
RewriteMap
),日志清楚地说明传递了什么密钥。示例:
<代码>(/var/log/http/rewrite.log)
地图查找 OK:map=host-redirects[txt] key=WHAT_IS_PARSED -> WHAT_IS_RETURNED
或
地图查找失败:map=host-redirects[txt] key=WHAT_IS_PARSED
使用 RewriteRule,| 之后的正确术语指定默认返回值。
在大多数情况下,默认的 404.html 页面或某种程度的。 非常有用。
所以在你的 httpd.conf 中,设置
RewriteLog 日志/rewrite.log
RewriteLofLevel 9
在你的虚拟主机中,你就完成了:)
BTW mapping may be the only way to debug "what is really matched"
You can set RewriteLogLevel to 9, the logs don't show what is parsed with
RewriteCond
.Instead it says whether "it matched" or not. Not very useful.
Using maps (
RewriteMap
), the logs clearly say what key is passed.Example :
(/var/log/http/rewrite.log)
map lookup OK: map=host-redirects[txt] key=WHAT_IS_PARSED -> WHAT_IS_RETURNED
or
map lookup FAILED: map=host-redirects[txt] key=WHAT_IS_PARSED
Using RewriteRule, the right term after | designates the default returned value.
In most cases, a default 404.html page or somewhat. Very useful.
So in your httpd.conf, set
RewriteLog logs/rewrite.log
RewriteLofLevel 9
in your virtualhost, and you're done :)