Monday, April 02, 2012

Part 2: URL-rewriting using IIS7, URL Rewrite, and your web.config file

Previously, I wrote about how I used the .htaccess file and Apache mod_rewrite to translate SEO-friendly links back to their original formats for interpretation. Later, I also needed to do the same thing for an IIS7 server. I decided to use URL Rewrite which works with IIS7 and IIS7.5. For this to work, though, the URL Rewrite module needs to be installed on your server. Using URL Rewrite, you can create rewrite rules like with an .htaccess file but you'll be using your web.config file instead.

Problem: SEO-friendly link interpretation was needed on an IIS7 server, which doesn't have mod_rewrite.

Impact: The links are hard to read and remember and are not very good for SEO.

Solution:
The rewrite rules are actually somewhat similar with URL Rewrite. I just needed to move the parts around and in some cases change the syntax a little. In the previous example with mod_rewrite I wanted the following:

Re-interpret "http://www.example.com/a/256/my-title"

back into "http://www.example.com/index.php?page=a&id=256&title=my-title".

For example, take the mod_rewrite rule:

RewriteRule ^(.*)/([0-9]+)/([a-z0-9_-]+)$ index.php?page=$1&id=$2&title=$3 [L]

This same rewrite rule will be written in the web.config file as:

<system.webServer>
  ... <!-- other stuff that belongs within these tags if you have it -->
  <rewrite>
    <rules>
      <rule name="My rewrite rule" stopProcessing="true">
        <match url="^(.*)/([0-9]+)/([a-z0-9_-]+)$" />
        <action type="Rewrite" url="index.php?page={R:1}&amp;id={R:2}&amp;title={R:3}" />
      </rule>
      <!-- create more rules here if you want -->
    </rules>
  </rewrite>
</system.webServer>

The rule begins with the line <rule ...>. The name part is just the name you give the rule and is arbitrary. The stopProcessing="true" is similar to the [L] flag in the mod_rewrite rule and tells the server to stop processing other (following) rules when this rule is executed.

The regular expression to match in the tag <match url=... /> is the same as the one to match in the .htaccess file. Using the example, the part of the URL "a/256/my-title" will match the (.*)/([0-9]+)/([a-z0-9_-]+) pattern (which is interpreted to be any-non-linebreak-char/number/alphanumeric-with_and-).

The last tag in the rule says what to do if there is a match. Notice that instead of the placeholders $1, $2, and $3 like for mod_rewrite, you have {R:1}, {R:2}, and {R:3}. {R:1} refers to the (.*) part of the regular expression, {R:2} refers to ([0-9]+), and {R:3} refers to  ([a-z0-9_-]+). Also note that the ampersands were changed to their entity references, &amp;.

In other words, the rule means: if the URL matches the pattern  (.*)/([0-9]+)/([a-z0-9_-]+), rewrite the URL to the format index.php?page=(.*)&id=([0-9]+)&title=([a-z0-9_-]+).

Now check out your links. Type in the SEO-friendly version in your browser. If your link works, congratulations! If you get an error, make sure all ampersands are changed to &amp; and that all of your rewrite rules are within <system.webServer> and not <system.web>. Also be sure that your XML is well-formed. If that still doesn't work make sure your IIS7/IIS7.5 server has URL Rewrite installed.

Sources:
Creating Rewrite Rules for the URL Rewrite Module, http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

1 comment:

  1. Just wanted to say thanks, this write up was exactly what I needed as I am regular expression challenged....

    ReplyDelete