views:

516

answers:

5

Hi all.

I wanted to know if there is a solution using IIS6 for an application to get rid of the default.aspx text in the url. so for example if a user hits:

www.website.com/default.aspx

the browser only shows:

www.website.com/

No matter what. It's just for SEO.

I already use UrlRewriting.NET for some rewrites in my app but for I'm not that clever to create a rule for that.

Any help is much appreciate.

Thanks. Jose

+1  A: 

If you have something to do URL rewriting, then all you need to do its ensure that your links point to the correct URL.

If you don't fix your links, its up to the browser to decide if it wants to display the actual link it requested.

If you would really like to do a dodgy job, you can use some javascript to make the address bar of the browser display whatever you like, even if its invalid.

Nippysaurus
A: 

The way I would do it is to use Application_BeginRequest in public class Global : System.Web.HttpApplication and check the HttpContext.Current.Request.URL for default.aspx, and then use HttpContext.Current.Response.Redirect from there if you find it.

The downside is having a redirect is not always so great and this isn't going to work if you are posting data to that default.aspx page. But you can't simply trick the browser into showing a different url, though you can tell ASP.NET to serve whatever page you want for any request.

jarrett
It doesn't work quite well, it could lead to infinite redirects.Thanks!
JRGuay
+1  A: 

If default.aspx is set as the default document to serve in IIS, and all your internal site links contain URL's without the default.aspx then I think that should do the trick.

Although the user can still type in default.aspx, search engine spiders should just pick up the friendlier URL's from your link href attributes.

Ian Oxley
A: 

I think ScottGu already has the topic of rewriting in ASP.NET covered: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx.

He covers things such as:

  • Rewriting using UrlRewriter.net, ISAPI Rewrite
  • ASP.NET Tricks, posting back (hitting the friendly version of the url)

With your problem I think you need to use a combination of, never linking to 'default.aspx' ie. ONLY link to '/'. Then use Scott's Form Postback browser file to ensure postbacks always hit that same friendly version of the url.

Redirecting 'default.aspx' to '/', which then gets served by 'default.aspx' sounds like a recipe for disaster to me. Just fix your links to ensure you never end up on 'default.aspx' explicitly.

Brendan Kowitz
Hi Brendan.I worked with Scott's article before to build the rules for SEO, however it doesn't apply for hiding the default page name. It just doesn't work.Thanks anyway.
JRGuay
+1  A: 

I think the simplest way to change the search results index (assuming it knows about HTTP 301) is to write a little function in your default.aspx's Page_Load that redirects the browser using a 301 Moved Permanently (or 302 Moved Temporarily).

void Page_Load(...) {

    if(Request.Path.EndsWith("default.aspx", true/*case-insensitive*/, null)) {
       Response.StatusCode = 301;
       Response.StatusDescription = "Moved Permanently";
       Response.Headers.Add("Location", "/");
       HttpContext.Current.ApplicationInstance.CompleteRequest(); // end the request
    }

    // do normal stuff here
}
Jeff Meatball Yang
Thanks Jeff.I think you're solution is quite good, but I got errors of infinite redirection.Thanks.
JRGuay