web.config redirect non-www to www

后端 未结 7 1950
北海茫月
北海茫月 2020-11-28 05:01

I need to redirect non-www urls to www url for both http and https urls. I tried following rules in web.config.



        
相关标签:
7条回答
  • 2020-11-28 05:28

    This post is basically for those who need to redirect non-www to www URL in windows hosting.

    In web.config file as below code:

    <system.webServer>
    <rewrite>
    <rules>
    <rule name=”Redirect example.com to www.example.com HTTP” patternSyntax=”ECMAScript” stopProcessing=”true”>
    <match url=”.*” />
    <conditions>
    <add input=”{HTTP_HOST}” pattern=”^example.com$” />
    <add input=”{HTTPS}” pattern=”off” />
    </conditions>
    <action type=”Redirect” url=”http://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
    </rule>
    <rule name=”Redirect domain.com to www.example.com HTTPS” patternSyntax=”ECMAScript” stopProcessing=”true”>
    <match url=”.*” />
    <conditions>
    <add input=”{HTTP_HOST}” pattern=”^domain.com$” />
    <add input=”{HTTPS}” pattern=”on” />
    </conditions>
    <action type=”Redirect” url=”https://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    

    In the above code please note there are two rules used one for http and another for https. To avoid clashing of http and https in the same rule, here used two separate patterns.

    I hope it works for you guys…!!

    0 讨论(0)
提交回复
热议问题