October
7th 2007
URL Rewriting in ASP

Posted under .net & Code & Software Design

“Rewriting” means intercepting a request from the web, and responding with the document behind a different URL. If this sounds like a redirect, it should; the main difference is that we’ll be doing this internally, without the client’s knowledge. ( The “client” is Internet Explorer, FireFox, Safari, or GoogleBot. ) In other words, www.example.com/page.aspx?search=software becomes exposed to the outside world as www.example.com/page/search/software; when somebody asks for the latter, the request is internally sent to the former.

You might do this after restructuring a web site, to prevent links to moved pages from turning into dead ends. Or, you might do it to improve your standing with search engines, the way most WordPress blogs use a post’s date and title, rather than it’s ID in the database, to create “permalinks.” On Apache servers, this is accomplished with mod_rewrite in the .htaccess file, which isn’t available to IIS users.

Instead, developers running Microsoft’s web server, Internet Information Services, or IIS, have three options for rewriting. Let’s examine these, in reverse order by difficulty and cleverness.

ISAPI Filters

Internet Services API has long been a way to change the server’s behavior, but not an ideal one. On the plus side, performance is better using ISAPI filters than either of the following methods; all things of course have a price, and here, it’s that filters must be written in C++ and compiled to a dll, which is then deployed to the server. CodeProject has an article on URL rewriting with ISAPI.

HttpHandlers

ASP.NET makes things easier. HttpHandlers are essentially ISAPI filters, with a similar but simplified model, and then assigned to a site through the web.config file. Redirecting files with an extension of ‘html’ to a corresponding aspx would look like this:

using System.Web;
public class StaticToDynamicRedirectHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        HttpRequest Request = context.Request;
        HttpResponse Response = context.Response;
        Server.Transfer(Request.Url.Replace(”.html”, “.aspx”);
    }
    public bool IsReusable {
        get { return false; }
    }
}

A Custom 404 Page

This will be the most familiar solution to the majority of ASP developers. A programmable “Page Not Found” page can go far beyond simply telling the user their request is bad. Of course a good 404 page should still tell the user about the problem, offer a search box, navigation, and so forth. But before any of this is sent to the client, you can examine the requested URL, and deal with it accordingly.

Taking the simple approach, we can use standard ASP or ASP.NET code to establish rules and conditions, like with mod_rewrite. We can map URLs and look them up against a database, XML, or a memory cache. We can parse the URL in whatever way the situation demands; only if these methods fail to come up with an acceptable replacement do we send the page and 404 response code.

If you’re using .net code, this goes in the beginning of the page_load event.

One Response to “URL Rewriting in ASP”

  1. Kyle Barron-Kraus on 07 Oct 2007 at 5:08 pm #

    This could come in handy when I’m working in ASP. Thanks :D

Trackback URI | Comments RSS

Leave a Reply