如何在重写条件中匹配 www 和非 www?

发布于 2024-10-09 15:47:13 字数 401 浏览 2 评论 0原文

我有一个重写映射,其中包含要重定向的域列表。目前我必须在重写映射中列出 www.foo.com 和 foo.com 。我想知道是否有一种方法可以在同一行中对 www 和非 www 进行 rewritecond 检查。

# Rewrite Map
foo.com file.php
www.foo.com file.php

# modrewrite
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

我尝试做 (www.)%{HTTP_HOST} 或 ^(www.)%{HTTP_HOST} 之类的事情,但没有运气。

I have a rewritemap that has a list of domains to redirect. Currently I have to list www.foo.com and foo.com in the rewrite map. I was wondering if there was a way to have the rewritecond check for both www and non-www in the same line.

# Rewrite Map
foo.com file.php
www.foo.com file.php

# modrewrite
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

I tried doing things like (www.)%{HTTP_HOST} or ^(www.)%{HTTP_HOST} but no luck.

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

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

发布评论

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

评论(4

柠檬色的秋千 2024-10-16 15:47:13

应该这样做:

RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteCond ${domainmappings:%2} ^(.+)$ [NC]
RewriteRule ^ /%1 [L,R=301]

第一个 RewriteCond 将删除可选的 www. 前缀。然后将余数用作第二个 RewriteCond 中重写映射的参数。

如果未找到匹配项,纯文本文件重写映射将返回空字符串:

如果找到该键,则映射函数构造将被替换为 SubstValue。如果未找到该键,则将其替换为 DefaultValue;如果未指定 DefaultValue,则将其替换为空字符串。

因此,如果满足第二个条件(请注意 ^(.+)$),则已找到匹配项,并且 %1 将包含 SubstValue(在本例中是file.php)。

This should do it:

RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteCond ${domainmappings:%2} ^(.+)$ [NC]
RewriteRule ^ /%1 [L,R=301]

The first RewriteCond will remove the optional www. prefix. The remainder is then used as parameter for the rewrite map in the second RewriteCond.

A plain text file rewrite map returns an empty string if no match is found:

If the key is found, the map-function construct is substituted by SubstValue. If the key is not found then it is substituted by DefaultValue or by the empty string if no DefaultValue was specified.

So if the second condition is fulfilled (note the ^(.+)$), a match has been found and %1 will contain the SubstValue (in this case file.php).

赢得她心 2024-10-16 15:47:13

从这里的帖子

http://www.eukhost。 com/forums/f15/simple-rewriterule-set-redirect-domain-6570/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.xyz.com$
RewriteRule ^(.*)$ http://www.xyz.com/test//$1 [R=301,L]

From a post here

http://www.eukhost.com/forums/f15/simple-rewriterule-set-redirect-domain-6570/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.xyz.com$
RewriteRule ^(.*)$ http://www.xyz.com/test//$1 [R=301,L]
背叛残局 2024-10-16 15:47:13

你可以尝试制作www.部分可选,包含以下内容:

# Rewrite Map
www.foo.com file.php

# modrewrite

# redirect to www domain always
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)$
RewriteRule (.*) http://www\.%1/$1 [L,R=301,QSA)

# redirect following the map
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

这将首先将 anything.anything 重定向到 www.anything.anything,然后将您的规则应用于下一个请求。虽然对重写映射不太了解,所以不能保证。

You can try to make the www. part optional with the following:

# Rewrite Map
www.foo.com file.php

# modrewrite

# redirect to www domain always
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)$
RewriteRule (.*) http://www\.%1/$1 [L,R=301,QSA)

# redirect following the map
RewriteCond ${domainmappings:%{HTTP_HOST}} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ www.domain.com/%1 [L,R=301]

This would first redirect anything.anything to www.anything.anything and then apply your rule on the next request. Not too knowledgeable with rewrite maps though so no guarantees.

夜司空 2024-10-16 15:47:13
RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]

RewriteCond %{HTTP_HOST} ^www.domain.com$

RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]

RewriteCond %{HTTP_HOST} ^www.domain.com$

RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文