Tuesday, December 9, 2008

Redirecting the Default Page (/default.aspx) to the Root (/)

The problem
Today I found that one of the sites I work on is being linked to from other sites in two different ways. Some links had default.aspx at the end, and some didn't. So what? Well, sure technically they both bring up the same page. But the search engines don't see it that way. This means that the link popularity for the home page was being split between two URL's. That's bad.

The search for a solution
So my search began for a solution. I knew that if I simply put the redirection code on default.aspx that I'd create an infinite loop. So I figured I'd put a statement in there to only redirect if "/default.aspx" is in the path. Unfortunately, I quickly found that ASP.NET sees both the "/" and the "/default.aspx" URL's as being the same and impossible to distinguish the difference. I heard some people say that it's possible to do this with an HTTPHandler in .NET, but without any examples I leaned more towards a solution on the IIS side of things. Now for all I know IIS 7 might be able to do what I want natively, but being stuck with IIS 6 it looked as though my only option was to install an ISAPI filter to take care of all this. So that's what I did.

The solution
I stumbled across Ionic's ISAPI Rewrite Filter (IIRF) which is completely free and powerful enough to do exactly what I needed. The information and download link is all found here:
http://www.codeplex.com/IIRF

Follow the instructions in the readme to install it on your IIS server and configure it for your website. You can use one of their sample "IsapiRewrite4.ini" files as a test and then go from there. But if you want to redirect default.aspx to the root of the site as I did, then read on.

I'll show you my ini file in my production environment first, and then explain how it works.

# IsapiRewrite4.ini
#
# ini file for the ISAPI rewriter.
#
# Tue, 9 Dec 2008 01:49
#

RewriteFilterPriority HIGH
RewriteLogLevel 0

# Any incoming URL that starts with /default.aspx will be redirected
# to the root directory while preserving any query strings.

RedirectRule ^/default\.aspx(.*) /$1 [I,R=301]

You can take the above and try it out for yourself and it may just work as is. But as always, I suggest testing thoroughly in a development environment before throwing this out on your live site.



Breaking it down
RewriteFilterPriority HIGH

In production I want IIRF to run as fast as possible since I'm essentially waiting on it to process the rules for every request. Albeit we're talking about imperceivable milliseconds but hey, I'm a speed freak.



RewriteLogLevel 0

This is actually the default setting. RewriteLogLevel 0 means there will be no logging at all done by IIRF. Again, this setting is for pure speed.



RedirectRule ^/default\.aspx(.*) /$1 [I,R=301]

There are a few things going on in this single line of code so allow me to break it down further.



RedirectRule

If you look through the IIRF examples you'll probably notice that they mostly use the RewriteRule command, while I'm using the RedirectRule command. Redirection is what we need here because we want IIRF to return a status code indicating that the page has moved (more on that later).



^/default\.aspx(.*)

This is the URL we're looking to redirect from. It's a parameterized regular expression that matches any URL starting with "/default.aspx"



/$1

This is the URL we're looking to redirect to (but only if the previous regular expression is satisfied). The "$1" part grabs the parameter from the regular expression and puts it into this URL. So in the event someone goes to "/default.aspx?animal=monkey" they will be redirected to "/?animal=monkey"



[I,R=301]

These are both modifiers documented in the IIRF readme. The "I" modifier specifies case-insensitive matching, which is what we want. And the "R=301" modifier specifies to use a 301 redirection, which is perfect for the search engines. The 301 status code specifies that a page has permanently moved. And that means that any link popularity from the old URL will be carried onto the new URL.



In effect, I've allowed the link popularity between "/" and "/default.aspx" to join forces, increasing the chances of the home page to place well in Google search results. At this point it's just a matter of time before Google re-indexes the site. I anticipate the PageRank and the traffic for the home page will go up so I'll be keeping an eye on it.