Rifled bore point of view

Code Samples,Search Engine Optimization

Using IIS URL Rewrite Extensions to Redirect Default.aspx

11 Dec , 2008  

About a week ago or so I received an email from Michael Wall asking me about an issue I have also noticed but had not mentioned in my earlier blog post about Canonical URL rewriting in IIS.  He asked,

“How can I redirect /default.aspx to / on IIS shared hosting without causing an endless loop?”

It’s a familiar problem with Windows Web Servers.  If you just try to apply a 301 redirect rule to the default document in IIS (which is usually a .NET page named Default.aspx) to send reqeusts back to the domain root, it will result in an endless loop of sending visitors back and forth between the two URIs.  http://www.yourdomain.com/ will redirect to http://www.yourdomain.com/Default.aspx (which is built-in behavior in IIS), and http://www.yourdomain.com/Default.aspx will just keep redirecting back to http://www.yourdomain.com/.

I had noticed this when working with IIS 6, but never really found an acceptable solution.  Being aware that Microsoft released the URL Rewrite Extension for IIS 7 last month, I figured I may as well ask someone in the know about it, so I emailed Bill Staples (GM of the Microsoft Web Platform) if he knew how this could be accomplished.

He sent my request to Ruslan Yakushev, the Project Manager for URL Rewriter whom he refers to as “a master at how to use it.”  I was very happy to get an email back from Ruslan with a solution made possible (and extremely simple) thanks to the URL Rewrite extenstion:

<rule name="Redirect" patternSyntax="Wildcard" stopProcessing="true">
<match url="default.aspx" />
<action type="Redirect" url="/" redirectType="Permanent" />
</rule>

While this will rather eloquently solve the issue if you administer IIS 7 on your server and can install the Microsoft module for URL Rewriting, it doesn’t necessarily answer Michael’s question on how to correct this problem on a shared server (i.e., one where the host may not install an IIS extenstion module for you).  It also won’t help anyone still using IIS 6; so if anyone out there has a workaround they’ve found for this issue, by all means please comment on this post or email it to me and I’ll publish it.

, , , , , , ,


12 Responses

  1. Barry Wise says:

    Mike;
    For SEO purposes, we only want one address to be the primary, or canonical, URL for a website. Otherwise, we end up with several URLs which Google has to struggle over to determine which one is the “real” one.

    For example, the following all look like the same website, but to search engines they are 4 completely different websites:

    http://www.itcn.com/
    http://itcn.com/
    http://www.itcn.com/Default.aspx
    http://itcn.com/Default.aspx

    We want redirects in place on 3 of these (and there may even be others) to all point to one canonical URL. So to a search engine, the HTTP headers would look like:

    200 OK http://www.itcn.com/
    301 [R] http://itcn.com/
    301 [R] http://www.itcn.com/Default.aspx
    301 [R] http://itcn.com/Default.aspx

    Three URLs give 301 redirect headers to one 200 (or OK) header.

  2. Barry Wise says:

    Wow Mike, thx …. I’m going to give this a try ASAP!!!!

  3. I’ve always had trouble with IIS and ASP doing this redirect. Glad you posted this!

  4. Scott says:

    Barry,
    Did Mike’s suggestion not work? I checked all of those URL’s and they still stand alone, not redirecting to the preferred domain. Any solution on the endless loop yet when working with IIS 6.0? I have a client in the same boat and he can’t get it figured out. Plus, we’re about to go through a site-wide URL re-write and they’re on IIS 6.0. I’ve seen mixed reviews on the challenges this can present for webmasters vs. IIS 7.

  5. I tried Mike’s solution – it kept on adding the ‘/default’ to the end of the url & so always ended up in a loop.

  6. Here is the code I was using (converted to vb.net from Mike’s c#)

    Dim hostname As String = Request.Url.AbsoluteUri.ToLower
    If hostname.EndsWith(“/default.aspx”) Then
    sNewURL = Left$(hostname, Len(hostname) – 12)
    Response.Redirect(sNewURL, False)
    Response.StatusCode = 301
    Response.End()
    End If

    sNewURL is set to the the same URL, but with the default.aspx removed. However, it .Net just adds the default.aspx straight back on. This test was done on my localhost machine, so it might be different on a www. url – but it would be great to have a solution that worked for both.

  7. Same problem over here with II6 as well. It seems somebody has a solution, I haven’t tried it yet though.. Just wanted to share the info with you and see your replies:

    http://swortham.blogspot.com/2008/12/redirecting-default-page-defaultaspx-to.html

  8. Hi. Can the IIS7 URL rewriter be used with Cassini server? I imagine not, but I wonder how to incorporate this useful feature into the development process. The Intelligencia offering allows development with Cassini and then deployment to a web server. It would be great to use the new IIS7 version instead.
    Thanks!

  9. Michael says:

    Hi Barry,

    Looks like the canonical url tag will take care of this headache.

  10. Tennerfest says:

    We definitely went down the IIS7 rewrite module route – however, as Michael points out, the canonical tag is also a good approach to take.

  11. Nothing worse than redirects that gets seen as unique entities, rather than one common page. 1000 links divided by 4 pages suddenly dramatically devalues your offering, making it vulnerable to being eclipsed in search rankings by your competitors. Thanks for the great advice offered here to prevent this guys!

  12. umesh chand says:

    Mr. Ruslan Yakushev solution works but it also creates problem.When i try to login from my default page it doesn’t work even when i use search button on master page it also not working,when i do the same thing from other pages everything works and when i remove that rule from my config then only it works from default page

Comments are closed.

Using IIS URL Rewrite Extensions to Redirect Default.aspx

by Barry Wise time to read: 2 min
12