Url Rewriting Options on IIS

17 Apr 2007

Apache users are spoiled when it comes to rewriting, thanks to the power of mod_rewrite. But on IIS, things are a little different so I thought I'd take some time out to explain the options. First off: why rewrite? My main reason is that I like having neat URLs, such as the ones on this blog. It's a rough edge that I like to round off. However, there are practical reasons for having neat URLs, such as readability and hackability. It's up to you to decide whether the extra effort is worth it, but I'm going to show you how little effort can be involved.

ASP.NET 2 actually has some rewriting capabilities built in, but the usefulness will depend on what you're trying to achieve. The urlMappings element in the web.config allows you map a particular aspx page to another aspx page, and include querystring parameters as you go. However, what it doesn't let you do is include dynamic values in the querystring, which lead James Avery to believe that ASP.NET 2.0 Url Rewriting crippled to the point of uselessness. For serious applications, I agree with him, but because it's built-in, I think small applications can still leverage this functionality.

You can roll your own rewriter using IIS and ASP.NET. This involves mapping all requests in IIS through the ASP.NET ISAPI module, and then coding up an HTTP Handler to route those requests as you require. This gives you a high level of flexibility, and means that you can code your rewrite rules in your .NET language of choice. However, this approach means that every resource is channeled through ASP.NET which is a bit unnecessary. It also means rolling your own solution (and there are plenty of articles to show you how), though there are plenty of similar things available for download that might fit your needs.

Then there are ISAPI module, which are written in super-fast C and purely geared for your rewrite needs. One example of this is HeliconTech's IsapiRewrite which uses an httpd.ini file to allow you to write mod_rewrite-like rules on a global or per-site basis. Each rule can incorporate regular expressions, giving excellent flexibility. Having used it in production I can recommend HeliconTech's solution.

A free alternative to the IsapiRewrite is IIRF, which also tries to emulate mod_rewrite. It supports PCRE regular expressions, and at the moment is my recommended solution to URL rewriting. Great price, great features, and very easy to install. It's even open source with a favourable license.

There is one footnote to the use of ISAPI modules for rewriting - depending on your hosting solution, you may not be able to register a new ISAPI module or map all requests to ASP.NET due to security restrictions imposed by your hosting provider. In this case you may be restricted to the first two solutions.

Feedback or questions on this post? Create an issue on GitHub.