A Better Canonical Domain Name Rule for IIS & Azure
09 Sep 2013
The default Canonical Domain Name rule in IIS doesn't work very well.
The generated rule doesn't work across different sub domains for example.
The rule that's generated in IIS produces this code in the web.config file.
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.tekcent\.com$" />
</conditions>
<action type="Redirect" url="http://www.tekcent.com/{R:1}" redirectType="Permanent" />
</rule>
This rule doesn't work for us since it redirects any request from localhost or staging.* to our production website. Here's an improved version of the rule that works across localhost, development, staging and production.
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^tekcent.com$" />
</conditions>
<action type="Redirect" url="http://www.tekcent.com/{R:1}" redirectType="Permanent" />
</rule>
This rule checks for an exact match on "tekcent.com" so sub domains such as staging.tekcent.com and localhost is allowed through. This works for us since canonical domain names is only required in production.