Using IIS URL Rewrite Extensions to Redirect Default.aspx

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.

Now go out and bombard the interweb with my self-whoring social links:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • MisterWong
  • StumbleUpon
  • Technorati
  • Diigo
  • FriendFeed
  • Identi.ca
  • LinkedIn
  • PDF
  • Tumblr
  • Twitter
  • Yahoo! Buzz

Tags: 301 redirect, asp.net, default.aspx, IIS, iis 7, microsoft, url rewriting, windows web server

Similar Posts:

Comments

15 Responses to “Using IIS URL Rewrite Extensions to Redirect Default.aspx”

  1. Mike Volodarsky Mike Volodarsky on December 11th, 2008 7:53 pm

    Hi Barry,

    Quick comment on the /default.aspx redirect problem – what are you looking to accomplish by performing a redirect to ‘/’?

    Typically ‘/’ performs either a rewrite to the default document (you are already on default.aspx, so I am guessing its not it), or performs a directory listing.

    If its the directory listing you are after, that requires to enable directory listing feature/disable default document feature (IIS 6.0 metabase only setting). If so, redirecting to ‘/’ from default.aspx will display the directory listing as expected.

    If you are trying to completely override the processing of ‘/’, you’ll need to use IIS 6.0 wildcard mapping and an IHttpModule module, which is typically not available in a hosted IIS 6.0 env …

    Best,

    Mike

  2. Barry Wise Barry Wise on December 11th, 2008 8:18 pm

    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.

  3. Mike Volodarsky Mike Volodarsky on December 11th, 2008 10:44 pm

    Hi Barry,

    Place the following into your default.aspx:

    void Page_Load(Object source, EventArgs e)
    {
    string hostname = Request.Url.DnsSafeHost;
    if (!(“www.itcn.com”.Equals(hostname, StringComparison.OrdinalIgnoreCase)
    && Request.Path.Equals(“/”)))
    {
    Response.Redirect(“http://www.itcn.com/”, false);
    Response.StatusCode = 301;
    Response.End();
    }
    }

    That should handle all the cases above with no IIS configuration changes …

    Thanks,

    Mike

  4. Barry Wise Barry Wise on December 11th, 2008 10:52 pm

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

  5. Mike Volodarsky Mike Volodarsky on December 12th, 2008 12:21 am

    Hope it helps – if not, shoot me a mail or let me know on my blog.

    Best,

    Mike

  6. GR Web Designs GR Web Designs on December 15th, 2008 1:04 pm

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

  7. Scott Scott on January 13th, 2009 6:11 pm

    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.

  8. Louis Gomez Louis Gomez on February 11th, 2009 6:03 pm

    Did anyone find a solution to this problem for IIS 6.0? I installed Helicon’s ISAPI filter and the redirect works but it creates issues with my asp.net gridview control. When you select a record, it merely reloads Default.aspx after the postback. Hmm, session state issue??

    Louis

  9. Cheap Flights Guy Cheap Flights Guy on February 27th, 2009 5:45 pm

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

  10. Cheap Flights Guy Cheap Flights Guy on February 27th, 2009 5:48 pm

    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.

  11. Hotels en Espagne Hotels en Espagne on April 2nd, 2009 5:11 pm

    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

  12. Learn how to play Las Vegas craps Learn how to play Las Vegas craps on June 18th, 2009 4:29 am

    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!

  13. Michael Michael on July 18th, 2009 5:30 am

    Hi Barry,

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

  14. Tennerfest Tennerfest on September 30th, 2009 4:05 am

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

  15. Jacques | SEO Expert Jacques | SEO Expert on October 29th, 2009 4:33 am

    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!

Leave a Reply