重定向非www网址到www

编程入门 行业动态 更新时间:2024-10-24 13:24:55
本文介绍了重定向非www网址到www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用赫利的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

本文发布于:2023-07-16 03:46:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   网址   www

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!