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.

Canonical Domain Name Rule

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 does'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.

An alternative method is to use web.config transforms - But it's a lot more work just to have something only for production use.

Monday June 17, 2013, By Anton Pham


comments powered by Disqus

Share this page

Archive