How to fix canonical domain name with a 301 redirect on IIS.


A ThatsIT Solutions Tutorial

Search engines treat websites that can be accessed by more than one domain name, such as www.contoso.com and contoso.com, as if they are two different sites. This affects the page ranking for the web site. Use this rule template to create a redirect rule that will enforce the use of a single domain name for the website.

The choice of www or non-www is purely yours to make, there is no technical issues regarding either format. It may be an advantage to use non-www if your domain name is already quite long. The www has no use or reason to exist, it is purely cosmetic. After making your decision you should make sure that all links in your site point to the format that reflects your choice in order to avoid un-necessary redirects.

Using IIS manager

The easiest way to fix canonical issues on Microsoft Windows Servers is using the url-rewire add-in for IIS. If you don’t have the URL rewrite tool you can download it using the Web Platform Installer. If you don't have access to the IIS Manager you can place the code at the bottom of this page directly in the web.config file.

  1. Open your IIS Manager and highlight the website you want to apply the rules to.
  2. Double click url-rewrite.
    Url rewrite
  3. Click add rules from the right menu.
  4. Select the canonical domain name rule.
    SEO rule templates
  5. Select your preferred domain name.
    Canonical domain name rule

That’s it all done. You can now look in your web.config and see the results.

In the web.config file

<system.webServer> 
    <rewrite>
        <rules>
        .........
        <rule name="CanonicalHostNameRule1">
            <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^thatsit\.com\.au$" negate="true" />
                </conditions>
            <action type="Redirect" url="http://thatsit.com.au/{R:1}" />
        </rule>
        .........
        </rules>
    </rewrite>
</system.webServer>