开发者

Best way to redirect websites in IIS 7

开发者 https://www.devze.com 2023-03-19 14:59 出处:网络
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:

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:

  1. 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).

  2. (Obviously) your site should accept those non-www subdomains.

  3. This rule need to be placed in web.config in website root folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消