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.
14 comments:
Hi,
I try to find how to use the ISAPI filter and I didn't find any solution for the loop of default.aspx.
But your blog helped me a lot!!!
Thank you VERY VERY much for your help.
I have only a question:
Do you know how to redirect pages like :
www.mydomain.com/(S(ulqqz555vqp2oeq121mhrl55))/
to www.mydomain.com ??
again thank you for your help
please answer me at my mail:
ephy At newreign DOT com
thanks
I haven't tested this, but I believe it would be something like this...
RedirectRule ^/\(S\(ulqqz555vqp2oeq121mhrl55\)\)/ / [I,R=301]
It's just a simple regular expression. The only trick is that you have to escape the parenthesis with a back slash.
thanks for your fast reply!
the case is that path is generated dynamically
so the path can be:
mydomain.com/(S(XXXXXX))/
thanks again
Ephy
In that case the rule should be...
RedirectRule ^/\(S\(.*\)\)/ / [I,R=301]
Hi Steve,
again thanks 4 your help.
I have a little problem with your rule:
i want to be able to change only the URL like
http://www.mydomain.com/Default.aspx
or
http://www.mydomain.com/XXXXX/Default.aspx (this work !)
but in any case, not change url like follow:
http://www.mydomain.com/Default.aspx?XXXX
thanks again 4 ur help.
see 2
I'm not sure I understand what you mean. It sounds like you're close though.
I'd suggest downloading The Regex Coach...
http://www.weitz.de/regex-coach/
It's a free app that highlights regular expression matches as you type. It's a great way to test and work out all the intricacies of what you're looking to do.
And if it helps, there's this site which has some great tutorials on regular expressions... http://www.regular-expressions.info/
Hi,
sorry if I wasn't clear...
the expression i need is:
redirect all page with:
default.aspx to / except page like
default.aspx?XXX
thanks again 4 your help
Oh, OK. I think this should do it...
RedirectRule ^/default\.aspx$ / [I,R=301]
It's work!!!
thank you very much for your help...
Greetings --
I'm using a .config file in .NET to acheive the same thing, like this:
add targetUrl="^~/default.aspx" destinationUrl="~/" permanent="true" ignoreCase="true"
It works well in our testing environment, but locks up our production server. I'm thinking it has to do with the fact that our production environment is load balanced while the development server isn't -- but I have no idea how to solve for this. Has anyone here run into an issue with load balanced servers and, if so, do you have a workaround/solution?
Thanks, in advance.
Dear steve,
I am a student from Taiwan, and my research is about OpenGL ES API now. Due to the paper, Simulation and development environment for mobile 3D graphics architectures, referred to the benchmark (terra 3d), and the benchmark is suitable for our research. We converted the benchmark from the OpenGL version into the OpenGL ES version and executed it in an OpenGL ES API developed by ourselves.
In addition, we will cooperates with cell-phone manufacturer, BROGENT, to port the benchmark on a developing platform, and further test the correctness about our API. However, the factory, BROGENT, is not a academic unit, so we need to check this benchmark is just for research, and get authorization from Mr. wortham.
thanks
name: Da-Jing Zhang-Jian
e-mail: salmoner.tw@yahoo.com.tw or s0922338@ncyu.edu.tw
@kelly - That sounds like an interesting solution you have there on your development server. I'm kinda surprised it works, actually. I figured it would have caused an infinite loop. Unfortunately, I have no clue why it isn't working on your other server.
@skald - Sounds like an interesting project. You have my permission to use Terra3D. Thanks for asking.
Hi,
Great post; however, we still cannot redirect ../default.asp to www.domainname.com using the sample .ini you have provided.
Is there any way you could possibly suggest what we might need to different with II6 and an ASP website?
Many thanks
Olly,
Well I think it should work assuming everything it set up properly.
Can you get any redirects to work with Ionic's filter or is it just this one?
Post a Comment