我有一个重写地图,其中包含要重定向的域列表.目前我必须在重写地图中列出www.foo.com和foo.com.我想知道是否有办法在同一行中对www和非www进行重写检查.
# 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}之类的东西,但没有运气.
这应该这样做:
原文链接:https://www.f2er.com/regex/356996.htmlRewriteCond %{HTTP_HOST} ^(www\.)?(.+) RewriteCond ${domainmappings:%2} ^(.+)$[NC] RewriteRule ^ /%1 [L,R=301]
第一个RewriteCond将删除可选的www.字首.然后将余数用作第二个RewriteCond中重写映射的参数.
如果未找到匹配项,则纯文本文件重写映射将返回空字符串:
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.
因此,如果满足第二个条件(注意^(.)$),则找到匹配,%1将包含SubstValue(在本例中为file.PHP).