Canonicalising Domain Names in IIS and ASP.NET

Canonicalising your domains (& subdomains) for your website is a crucial practice to follow if SEO is important for your site. Even if you are not, it scores you points from a usability point of view - one endpoint for your users. Normally, canonicalising domains for an ASP.NET website on IIS is a straight forward affair, assuming you have access to IIS that the site is being installed on, or that you don't but you know that IIS has the URL Rewrite module installed. If you can't use this method, then we may just have an alternative for you too!

If you do have access to IIS, look for the follwoing icon:

If this isn't there, you can download it from the Web Platform Installer or the iis.net site. Once you have the URL Rewrite module installed, you can add a rule to your webconfig to canonicalise the domain:

  <system.webServer>
    <rewrite>
      <rules>       
        <rule name="301 Redirects" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www.trycatchexfinally.co.uk$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www\.trycatchexfinally\.co\.uk/{R:0}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

So all this rule says is for all urls:

<match url="(.*)" />

Is the http host = www.trycatchexfinally.co.uk (our canonical domain)?

If it is, exit (negate = true), if not then redirect to it. If you use something like Fiddler to look at how this works in terms of requests, you should see this if you try to go to a domain that is bound to your site that is not the canonical:

 

 

Importantly, the rule uses a 301 (permanent) redirect, so that search engines know that the content it is trying to reach will always be on your canonical domain.

Job Done!

Okay, but I don't have root access to IIS to set up the URL Rewrite module.

If this is the case, then you will need to get your application to handle the canonicalisation. The easiest way is to check the http host in your begin_request method that can be put in your global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.Host != System.Configuration.ConfigurationManager.AppSettings["CanonicalDomain"])
                Response.RedirectPermanent(string.Format("{0}://{1}{2}",
                        HttpContext.Current.Request.Url.Scheme,
                        System.Configuration.ConfigurationManager.AppSettings["CanonicalDomain"],
                        HttpContext.Current.Request.RawUrl), true);
        }

This does the same thing as the URL rewrite rule - checks to see if the http host matches your canonical domain, and if not, it performs a redirect.

I'm new to SEO, why does this all matter?

If you have multiple domains or subdomains pointing at the same content, on the off chance that a search engine crawls the content under different domains, it will conclude that you have duplicate content (content that has been duplicated from another website). If you know anything about SEO you will know duplicate content is very bad (if you don't know this, a quick Google search should clear things up) and will impact your search rankings. This article has covered the very basics of canonicalising domains, and though there are other techniques out there, this is quick and easy to implement on any ASP.NET website.



Tagged: IIS, Canonical Domains, ASP.NET,
Categorised: Technical SEO,
By:
On: