JRGuay
JRGuay

Reputation: 226

Hiding default.aspx from the URL

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

Upvotes: 3

Views: 6005

Answers (5)

Brendan Kowitz
Brendan Kowitz

Reputation: 1795

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.

Upvotes: 3

Jeff Meatball Yang
Jeff Meatball Yang

Reputation: 39027

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
}

Upvotes: 2

Ian Oxley
Ian Oxley

Reputation: 11056

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.

Upvotes: 1

Jarrett Widman
Jarrett Widman

Reputation: 6419

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.

Upvotes: 0

Nippysaurus
Nippysaurus

Reputation: 20378

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.

Upvotes: 1

Related Questions