0

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!

Jeff
  • 13,079
  • 23
  • 71
  • 102

3 Answers3

1

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
  • 22,633
  • 10
  • 99
  • 115
  • 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. – Jeff Jan 14 '10 at 03:51
  • Make sure you set 404 handler in IIS, not in web.config. – Pavel Chuchuva Jan 14 '10 at 04:08
  • 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... – Jeff Jan 14 '10 at 04:16
  • 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 Jan 14 '10 at 21:39
  • 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. – Jeff Jan 14 '10 at 23:29
  • 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. – Jeff Jan 14 '10 at 23:44
  • Ok I figured out that I need to use Request["aspxerrorpath"] to read the path. Thanks!!! – Jeff Jan 14 '10 at 23:52
  • It's so obvious to get value from QueryString I don't know what I was thinking... ^_^ – Jeff Jan 14 '10 at 23:57
0

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
  • 16,471
  • 11
  • 61
  • 86
  • 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. – Jeff Jan 14 '10 at 01:13
0
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
  • 15,108
  • 7
  • 50
  • 91