在 RewriteRule 中匹配 HTTP_HOST,而不是使用 RewriteCond

发布于 2024-10-04 16:48:52 字数 936 浏览 0 评论 0原文

我需要制作一堆 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 技术交流群。

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

发布评论

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

评论(2

灰色世界里的红玫瑰 2024-10-11 16:48:52

Apache 文档中关于 RewriteRule 的描述如下:

匹配什么?

该模式最初将与主机名和端口之后、查询字符串之前的 URL 部分进行匹配。 如果您希望匹配主机名、端口或查询字符串,请使用带有 %{HTTP_HOST}、%{SERVER_PORT} 或 %{QUERY_STRING} 变量的 RewriteCond分别。

但是,如果您能够将规则放入 apache-config 中,则还有另一种简写方法:您可以使用 RewriteMap。

RewriteMap     host-redirects   txt:/etc/apache2/host-redirects.map
RewriteRule ^/([^/]*).htm$ http://%{HTTP_HOST}/${host-redirects:%{HTTP_HOST}/$1|$1} [R,L]

host-redirects.map 看起来像这样:

www.domain1.com/old-url-1a.htm /new-url-1a/
www.domain1.com/old-url-1b.htm /new-url-1b/
www.domain2.com/old-url-2a.htm /new-url-2a/

The Apache Documentation says about RewriteRule:

What is matched?

The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

But if you are able to put the rules inside your apache-config, there is another shorthand way: You could use a RewriteMap.

RewriteMap     host-redirects   txt:/etc/apache2/host-redirects.map
RewriteRule ^/([^/]*).htm$ http://%{HTTP_HOST}/${host-redirects:%{HTTP_HOST}/$1|$1} [R,L]

The host-redirects.map would look like this:

www.domain1.com/old-url-1a.htm /new-url-1a/
www.domain1.com/old-url-1b.htm /new-url-1b/
www.domain2.com/old-url-2a.htm /new-url-2a/
孤凫 2024-10-11 16:48:52

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 :)

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