Redirect domain without www to www with web.config

It is been long time user keep on asking how to redirect the domain name with www only from their website.

To get this fixed from IIS web.config file, you may copy the following script to your web.config file to redirect them from without www to www.

<rule name="www redirect" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="www redirect https" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="on" />
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

With the above code, it will redirect you without www to www even you have https configured.

Published by

Mick Genie

Mick Genie is the founder of MickGenie.com and working at ExaBytes Network Sdn Bhd and WPWebHost web host company. He is expertise in Windows and Linux environment especially web hosting related information, tips and trick as well as the IT Information.