我使用赫利的ISAPI重写3 ,基本上实现的.htaccess在IIS中。我需要一个非www URL重定向到www的版本,即example应该重定向到www.example。我使用的例子下面的规则,但它会影响子域:
I'm using Helicon's ISAPI Rewrite 3, which basically enables .htaccess in IIS. I need to redirect a non-www URL to the www version, i.e. example should redirect to www.example. I used the following rule from the examples but it affects subdomains:
RewriteCond %{HTTPS} (on)? RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC] RewriteCond %{REQUEST_URI} (.+) RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]这适用于大多数情况,但也重定向sub.example到www.sub.example。我怎样才能把上面的规则,使子域不会被重定向?
This works for most part, but is also redirect sub.example to www.sub.example. How can I rewrite the above rule so that subdomains do not get redirected?
推荐答案附加以下的RewriteCond:
Append the following RewriteCond:
RewriteCond %{HTTP:Host} ^[^.]+\.[a-z]{2,5}$ [NC]
这样,它只是将规则应用于nondottedsomething.uptofiveletters你可以看到,subdomain.domain将不匹配的情况,因而不会被改写。
That way it'll only apply the rule to nondottedsomething.uptofiveletters as you can see, subdomain.domain will not match the condition and thus will not be rewritten.
您可以更改[AZ] {2,5}进行更严格的TLD匹配的正则表达式,以及将所有的约束条件在域名允许的字符(如[^] +是比严格意义上更加宽容)。
You can change [a-z]{2,5} for a stricter tld matching regex, as well as placing all the constraints for allowed chars in domain names (as [^.]+ is more permissive than strictly necessary).
总之,我认为在这种情况下,将没有必要。
All in all I think in this case that wouldn't be necessary.
编辑:萨迪发现缺陷的正则表达式,更改从它的第一部分[^]至[^] +
sadie spotted a flaw on the regex, changed the first part of it from [^.] to [^.]+
更多推荐
重定向非www网址到www
发布评论