How To: Non-WWW to WWW 301 Redirects in ASP.NET

Posted Monday December 10, 2012 at 12:15:15 am in Development

There are enough articles out there about SEO and having duplicate content, i.e. http://domain.com/productA.aspx and http://www.domain.com/productA.aspx. Even in Google's Webmaster Tools there's an option of setting a preferred domain:

http://support.google.com/webmasters/bin/answer.py?hl=en&answer=44231

They state:

The preferred domain is the one that you would liked used to index your site's pages (sometimes this is referred to as the canonical domain). Links may point to your site using both the www and non-www versions of the URL (for instance, http://www.example.com and http://example.com). The preferred domain is the version that you want used for your site in the search results.

To accommodate both a non-www domain and a www domain whilst not having duplicate content you can make a couple of quick changes to your web.config file. This assumes that you have the URL Rewrite module installed. It can be installed through the Web Platform Installer. More information is available here:

http://www.iis.net/downloads/microsoft/url-rewrite

Once you have that installed, you can edit your web.config manually (or by using the GUI inside of the IIS Manager) by adding the following to the system.webServer section:

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

That above XML snippet will have http://domain.com redirect (301 redirect, i.e. permanently) to http://www.domain.com for HTTP. If you want to support both HTTP and HTTPS you can do the following:

<rewrite>
      <rules>
		    <rule name="Redirect domain.com to www.domain.com HTTP" patternSyntax="ECMAScript" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" appendQueryString="true"/>
        </rule>
		    <rule name="Redirect domain.com to www.domain.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.domain.com/{R:0}" redirectType="Permanent" appendQueryString="true"/>
        </rule>
	    </rules>
    </rewrite>

Both of the above snippets are using regular expressions to handle the pattern matching.

Happy coding!

The opinions expressed herein are my own personal opinions and do not represent my employer’s view