views:

383

answers:

3

I am using ASP.NET 2.0 on IIS6 therefore I can’t use system.web.routing. I’ve tried a few URL rewriters but none did what I wanted.

I basically need to transform (read/parse then redirect) URL 1 into 2 with minimum IIS configuration because I don't have the full authority to reconfigure web servers (i.e. ISAP on IIS6 or install 3rd party extensions/libraries). And I can’t transform URL into 3 because all the physical links will break.

  1. http://www.url.com/abc123
  2. http://www.url.com/default.aspx?code=abc123
  3. http://www.url.com/abc123/default.aspx?code=abc123

Thank you!

A: 

I would suggest that you look at using a custom transform with UrlRewriter.net. This link details how you can do it with the Intelligencia.UrlRewriter assembly (about 3/4 of the way down the page). Hopefully is good enough to do what you need.

Kane
I don't think URL rewrite will work because I don't want the URL to be rewritten I only need the URL to be read/parsed then redirect.
Jeffrey C
A: 
protected void Page_Load(object sender, EventArgs e)
{
    HtmlLink canonicalTag = new HtmlLink();
    canonicalTag.Href = "http://www.url.com/default.aspx";
    canonicalTag.Attributes["rel"] = "canonical";
    Page.Header.Controls.Add(canonicalTag);
}

http://codersbarn.com/post/2009/02/21/ASPNET-SEO-and-the-Canonical-Tag.aspx

IrishChieftain
+1  A: 

Create 404 error handler, e.g. 404.aspx. In that page, parse request URL and extract code using Request.Path. Then, redirect to default.aspx?code=abc123.

You should be able to set 404 (page not found) handler to 404.aspx for your website, most hosting providers allow that.

Pavel Chuchuva
I was going to manually do the parsing and redirect in Application_Error (global.asax) because I couldn't get rid of the aspxerrorpath querry string as I can't capture the value of /abc123 before aspxerrorpath is thrown because Request.Path is alwasy the URL of the 404 error page.
Jeffrey C
Make sure you set 404 handler in IIS, not in web.config.
Pavel Chuchuva
Do you recommend Application_Error (global.asax) than 404? Becuase I don't have too much control over web servers and we have more than 1 web servers so I prefer the "soft" way. For any IIS setting change, it takes weeks to get approved...
Jeffrey C
If you get Application_Error event for extension-less URLs like `http://www.url.com/abc123` by all means, use it. Otherwise you need to configure IIS to use your 404 handler.
Pavel Chuchuva
I've configured my dev IIS to forward 404 to my 404 page yet Request.Url and Request.Path both showing the path to 404 page and Request.UrlReferrer is null.
Jeffrey C
I sort of don't want to use ApplicationError unless I have no other choice becuase by doing "URL transofmration" in AppError has performance impacts I believe.
Jeffrey C
Ok I figured out that I need to use Request["aspxerrorpath"] to read the path. Thanks!!!
Jeffrey C
It's so obvious to get value from QueryString I don't know what I was thinking... ^_^
Jeffrey C
Actually... on IIS I need to manually parse PathAndQuery to get my key...
Jeffrey C