如何在重写条件中匹配 www 和非 www?
我有一个重写映射,其中包含要重定向的域列表。目前我必须在重写映射中列出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该这样做:
第一个
RewriteCond
将删除可选的www.
前缀。然后将余数用作第二个RewriteCond
中重写映射的参数。如果未找到匹配项,纯文本文件重写映射将返回空字符串:
因此,如果满足第二个条件(请注意
^(.+)$
),则已找到匹配项,并且%1
将包含 SubstValue(在本例中是file.php
)。This should do it:
The first
RewriteCond
will remove the optionalwww.
prefix. The remainder is then used as parameter for the rewrite map in the secondRewriteCond
.A plain text file rewrite map returns an empty string if no match is found:
So if the second condition is fulfilled (note the
^(.+)$
), a match has been found and%1
will contain the SubstValue (in this casefile.php
).从这里的帖子
http://www.eukhost。 com/forums/f15/simple-rewriterule-set-redirect-domain-6570/
From a post here
http://www.eukhost.com/forums/f15/simple-rewriterule-set-redirect-domain-6570/
你可以尝试制作www.部分可选,包含以下内容:
这将首先将
anything.anything
重定向到www.anything.anything
,然后将您的规则应用于下一个请求。虽然对重写映射不太了解,所以不能保证。You can try to make the www. part optional with the following:
This would first redirect
anything.anything
towww.anything.anything
and then apply your rule on the next request. Not too knowledgeable with rewrite maps though so no guarantees.