How to redirect from "/default.aspx" to "/" on IIS?

[ List of other entries | oyoy.eu main page ]
 

On Apache servers it is easy to provide a server side redirect from the explicit file to the domain root, eg from "/index.htm" to "/". This kind of redirect is not possible (at least not easily) on IIS / ASP or ASP.NET. In the server side code you cannot recognize whether or not a user has accessed the page through the default file handler (eg "/") or through the file name explicitly ("/default.aspx"). In both situations, the server will tell the script that the user is using the file "/default.aspx".

One solution to this dilema is to use client side scripting: javascript. A simple script could be the following:

<script type="text/javascript"><!--
var u = 'http://mydomain.com/';
if (self.parent.frames.length != 0) self.parent.location=u;
if (self.location != u) self.location=u;
//--></script>

This script, placed in the default file for mydomain.com, will automatically redirect to the root URL ("/") whenever a user tries to access the filename explicitly. Additionally, it will redirect in the following situations:

Technically, a client-side redirect is not treated as a redirect by the search engines. However, with this kind of redirect in place, the user will always see the proper URL in his browser and therefore is more likely to use the proper URL for a link to the page.

Warning: Do not use a redirect like this on a page that is used as a landing page for Google Adwords with auto-tagging. The tagging will trigger a redirect, which will re-trigger when the user wants to use the browser back-button to go back. It could be seen as an attempt to lock a user to the site. It is preferable to create a separate landing-page for Adwords or other PPC campaigns.