Domain rewrites in IIS web.config

Sites are often accessible from multiple domain names, for example a company may have purchased the .co.uk and .com versions of their domain name so as to 'own' that domain and prevent others squatting on that domain. It was also a convention that websites be served from the www subdomain, although this has been relaxed in recent years with the likes of Twitter making it popular to serve websites from bare domains.

Serving websites from multiple domains is often detrimental to a site's search ranking, as search engines see this as two sites serving duplicate content. Rewriting multiple domains to a single domain allows a site to respond to multiple domain names, but all content is served from a single domain.

The example below allows a visitor to enter some-domain.co.uk, www.some-domain.co.uk, some-domain.com, and www.some-domain.com into their browser and be redirected to www.some-domain.co.uk.

In IIS this is configured in the <system.webServer> section of web.config.

<!-- START: Rewrite rules first added 03 October 2013 -->
<rewrite>
  <rules>
	  <rule name="Redirect non-www to www" patternSyntax="ECMAScript" stopProcessing="true">
		  <match url=".*" />
		  <conditions logicalGrouping="MatchAny">
			  <add input="{HTTP_HOST}" pattern="^some-domain.co.uk$" />
			  <add input="{HTTP_HOST}" pattern="^some-domain.com$" />
			  <add input="{HTTP_HOST}" pattern="^www.some-domain.com$" />
		  </conditions>
		  <action type="Redirect" url="http://www.some-domain.co.uk/{R:0}" redirectType="Permanent" />
	  </rule>
  </rules>
</rewrite>
<!-- END: Rewrite rules -->