重定向非www网址到www [英] Redirecting non-www URL to www

查看:267
本文介绍了重定向非www网址到www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用赫利的ISAPI重写3 ,基本上实现的.htaccess在IIS中。我需要一个非www URL重定向到www的版本,即example.com应该重定向到www.example.com。我使用的例子下面的规则,但它会影响子域:

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.com should redirect to www.example.com. 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.com到www.sub.example.com。我怎样才能把上面的规则,使子域不会被重定向?

This works for most part, but is also redirect sub.example.com to www.sub.example.com. 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.com将不匹配的情况,因而不会被改写。

That way it'll only apply the rule to nondottedsomething.uptofiveletters as you can see, subdomain.domain.com 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆