Sunday 21 June 2015

Redirect Non-WWW to WWW in Web.config

Here is the URL Rewrite Rule in the IIS web.config file that redirects non-www requests to www requests for your domain name. Make sure you replace example.com with the name of your domain.
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:0}"
                  redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer> 
</configuration>