How to Fix: Canonical Urls with IIS 7’s Url Rewrite feature for https

by Steve French in ASP.net, ASP.net MVC, How To Fix, SEO on June 1, 2010

SATOR Magic squareThe Problem: You want to make your website all SEO friendly by creating a single, canonical url.  For example, if someone types in https://www.stronico.com, you want them to be redirected to https://stronico.com (Google likes it this way).  You do some research and discover that all of the default code and documentation for handling canonical Urls in IIS 7 uses web.config files and the URL Rewrite application program.  All is well and good so far, but what if you use SSL?  The stock code will always redirect you to https://stronico.com/Signup/ even if the original url was https://stronico.com/Signup/ (note the https).

The Cause: URL Rewrite is new, and it was an oversight on Microsoft’s part.

The Solution: You have to use two rules, first checking to see if the url contains the https.  Here is a sample below

<rewrite>
<rules>
<rule name=”Canonical Hostname – No HTTPS”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false”>
<add input=”{HTTPS}” pattern=”OFF” />
<add input=”{HTTP_HOST}” pattern=”^stronico.com$” negate=”true” />
</conditions>
<action type=”Redirect” url=”https://stronico.com/{R:1}” />
</rule>
<rule name=”Canonical Hostname – With HTTPS”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false”>
<add input=”{HTTPS}” pattern=”ON” />
<add input=”{HTTP_HOST}” pattern=”^stronico.com$” negate=”true” />
</conditions>
<action type=”Redirect” url=”https://stronico.com/{R:1}” />
</rule>
</rules>
</rewrite>

That’s it! It took me an embarassingly long time to figure that one out.
Creative Commons License photo credit: Marco Fedele

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog