Rewriting Urls with IIS7

Recently we’ve played a story on savings around cleaning the URL if it does not conform to the standards set out by the SEO team.

Problem situations:

http://www.somewebsite.com/link needs to be  http://www.somewebsite.com/link/

or

http://www.somewebsite.com/LINK needs to be http://www.somewebsite.com/link/

To solve this, we were pointed in the direction of the IIS7 Url Rewriting module by the another team at work.

IIS7 Rewriting Module

The module allows you to rewrite a URL before it hits your application and can redirect the user with a HTTP status code as required. This article goes into the theory but there are lots of examples and tutorials here.

This diagram from the tutorial article gives the best overview of what goes on in the pipeline; http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

To get started and use the module you will need to install it from here.

Then its as simple as adding the configuration to your applications web.config.

<rewrite>
<rules>
<rule name=”Add trailing slash and to lower (only with no forward slash)” enabled=”true” stopProcessing=”true”>
<match url=”.*[^/]$” />
<conditions logicalGrouping=”MatchAll”>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
<add input=”{HTTP_METHOD}” pattern=”GET” />
<add input=”{URL}” negate=”true” pattern=”.*savings/combres.axd.*” />
</conditions>
<action type=”Redirect” url=”{ToLower:{R:0}}/” redirectType=”Permanent” />
</rule>
<rule name=”Convert to lower case” stopProcessing=”true”>
<match url=”.*[A-Z].*” ignoreCase=”false” />
<conditions logicalGrouping=”MatchAll”>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
<add input=”{HTTP_METHOD}” pattern=”GET” />
<add input=”{URL}” negate=”true” pattern=”.*savings/combres.axd.*” />
</conditions>
<action type=”Redirect” url=”{ToLower:{R:0}}” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>

This configuration adds a trailing backslash to any URL that does not have a trailing backslash and lowers all URL’s that contain uppercase characters. There is also the <action /> tag which performs a permanent redirect.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: