IIS 7 URL Rewrite and Canonical URLs

What the heck is a “canonical URL?”  It sounds vaguely like a website the Pope would decree as required reading.  At least in Biblical terms.  But in computer terms, “canonical” simply means “the normal way stuff is.”  It comes from mathematics, where a standard format for writing equations is important, but it’s rarely used that way anymore.  And who really cares anyway?

For our use, a “canonical URL” is simply a standard URL for accessing your web site.  In search engine optimization it means that no matter how a user gets to your web site, it displays the exact same page with the exact same URL so that search engines know the page is a single page, no matter how many URLs point to it.  A basic example:

http://www.foo.bar and foo.bar are the same site.  You and I can tell that, because both URLs bring up the same page.  And everyone knows that the “www” host name can be dropped from a URL and still get to the site, right?  After all, your business card doesn’t include the www, and neither does your phone book listing.  That’s the normal way this stuff works.  The canonical way.  Except they are two different pages.

Computers don’t make leaps of faith and assume that whether or not a www precedes a URL makes no difference.  To a computer, they are two separate URLs, one with the www and one without.  You and I know they’re really the same thing, but computers are stubborn.  And picky.  And for SEO, stubborn and picky are not the best attributes because SEO is for people, some of whom may be stubborn and picky (these ones are often referred to as “ex-girlfriends”) but most of whom are not.  At least to the totally anal extent of a computer.  So we fix this by providing canonical URLs, ones that, to a computer, are exactly the same.  In simple terms, we convert any request for foo.bar into http://www.foo.bar (or vice-versa) before we let the search engine see it.  That way, a search engine sees one page even though there are two URLs that can reach it.  Instead of splitting the popularity of the page into two separate pages, the combined popularity score reflects what happens in real life.

And an easy way to do this is with the URL Rewrite module for IIS 7.  We’re not going to run through the setup and use of this module, you can find that information at Microsoft’s IIS web site, just a quick listing of the rewrite rule you enter in your web.config file.  Which would look something like this:

<rule name=”Redirect URL to WWW version”
  <match url=”.*” />
      <add input=”{HTTP_HOST}” pattern=”^foo.bar$” />
  <action type=”Redirect” url=”http://www.foo.bar/{R:0}”
        redirectType=”Permanent” />

Some quick notes:

stopProcessing=”true” — This stops processing any rewrite rules after this one is triggered.  Normally you would want to do this, but if you have further rules that need processing, like changing a query string to a friendly URL, set this to false.

redirectType=”Permanent” — This provides a 301 HTTP response to the requesting client, indicating that the foo.bar URL has been permanently redirected, or moved, to the http://www.foo.bar URL.  Search engine spiders are smart enough to make this a permanent change in their search results, in essence they correct older search links that didn’t have the www in the URL.

Canonical URLs will help your page ranking in search results, but this is not the only SEO technique that can be handled with the URL Rewrite module.  But it is a simple function you can configure once for the site and never worry about again.  To find other possibilities for SEO use of the IIS 7 URL Rewrite module, be sure to check Carlos Aguilar’s blog and the URL Rewrite Module forum at www.iis.net.


SEO is not rocket science and it’s hard to screw up using the IIS 7 Rewrite module when you follow the documentation, but if you do it’s not our fault.  If you’re an offended ex-girlfriend who is angry at this post, might we suggest counseling to get past the anger and hurt and move on with your life.  There are plenty of guys out there who aren’t dirt bags…  Well, aren’t usually dirt bags.  After all, we’re guys.  And by definition we’re pretty much scum one way or the other.  And unfortunately, we stopped maturing in our early teens, though many of us hide our immaturity well enough to hold stable relationships.  At least until we do something stupid again.  That’s why florists and jewelers make such a good living.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s