How to Fix: Canonical Urls with IIS 7's Url Rewrite feature for https - Digital Tool Factory blog

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

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 http://www.stronico.com, you want them to be redirected to http://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 http://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=”http://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

 

Written By Steve French

 

Leave a Reply

Your email address will not be published. Required fields are marked *






Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.