What is the best way to make sure all pages hit on a website hosting in IIS 7 are redirecting with the www. sub-domain...examples below:
http://site.com ==> http://www.site.com
开发者_运维问答
or
http://site.com/somepage.html ==> http://www.site.com/somepage.html
This rule will redirect (301 Permanent Redirect) all requests to http://site.com/somepage.html
into http://www.site.com/somepage.html
(will work if domain name = site.com
):
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^site\.com$" />
</conditions>
<action type="Redirect" url="http://www.site.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
This one will do the same but will redirect from ANY SUBDOMAIN to www (e.g. hello.site.com => www.site.com
):
<system.webServer>
<rewrite>
<rules>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.site\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.site.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
NOTES:
You will need URL Rewrite v2 module to be present (should work with v1.x as well, but I cannot test it -- only v2 around).
(Obviously) your site should accept those non-www subdomains.
This rule need to be placed in
web.config
in website root folder.
精彩评论