mod_rewrite for IIS

[Update, 2008 June 18: Due to very strong interest, this rewriting filter was moved to an open-source project hosted on codeplex, in 2006. Called IIRF, it is the closest thing to Apache mod_rewrite for IIS.  It now includes a number of advanced features, and can be used with wordpress, PHP, JSP, ASPX, joomla, and many other server technologies. I've been maintaining it with regular releases and updates, and it's still FREE. Find it at www.codeplex.com/IIRF]

Something worth checking out - if you have used the Apache web server, or even the ASP.NET HTTP Handler (.ashx) article1 , you may be aware of the concept of rewriting URLs. The idea is that a browser or other web client sends a simple URL request to a server, and then on the server side, that inoming URL can be mapped or transformed to a totally different URL, possibly a much more complex URL, on the server. A request for http://server/MyPicture.jpg sure looks like it refers to a static file on the server. But on the server, that request might get mapped to TheNewPicture.jpg, or might even be handled by program logic that dynamically generates the requested image, performs some auditing, and so on.

Why Map or Rewrite URLs?

Mapping between client-side, browser-visible URLs and the actual server-side URLs - Why would anyone want to do this? Lots of reasons, including - maybe you want to modify your site, and change the way you handle certain requests, but don't want to change the original URLs. Another very common reason is that some portal and community site frameworks introduce really ugly, lengthy, hard to remember URLs, and you might want to spare your users the need to type all those things in. A related reason - some search crawlers won't index dynamic pages - pages that have more than a few parameters in the query string. A dynamic page like page.aspx?id=7182&map=17&uid=jueww&confirm=diu8hjfdj might get passed over by the search engine crawler, whereas a page with a simple URL like Confirmation.aspx might get indexed. There are lots of other reasons, too. Maybe you want to provide different results depending on the requesting host, or the time of day, or based on some other conditions. You may be thinking - "I can do all of these things through some relatively simple custom server-side logic," and you are correct. URL Rewriting is just another option, and it may or may not be interesting or useful to you. IT may be a complement to, or a replacement of, custom server-side logic, whether you use ASP.NET, JSP, PHP, or something else.

Apache's Mod_Rewrite

There is an extension module for the Apache HTTP Server that provides a declarative way to rewrite URLs: mod_rewrite. It's very powerful and pretty well known. The way it works: you configure all the rules for rewriting URLs in a text file. At runtime, the module parses the text file and then applies those rewrite rules to incoming URLs. A simple model, though the configuration can be tricky.

You can match incoming URLs based on Regular Expressions, which is a language unto itself. You can apply conditions to rewrite rules - Rules might or might not be applied, depending on the requesting host, the time of day, or the state of the filesystem. You can chain conditions together. Like I said, powerful, but tricky.

It works only on Apache.

What about IIS?

I've heard from a number of IIS users and administrators that would like similar capability on IIS, and would like it for a similar cost (free).

ASP.NET HTTP Handlers can be used to do URL Rewriting. If you like, you can map *any* extension to your custom HTTP Handler, and handle it in .NET code. So, stuff like .jpg images or other binary types can be handled in managed code. That's nice, but some people do not run ASP.NET on their servers, and don't want to. Also, using the .ASHX approach essentially means the declarative model of Mod_rewrite is replaced by a programmatic model - in other words you would have to be a developer to use a rewriter build on this stuff. There are third parties who have written configurable rewriters based on ASP.NET in the spirit of mod_rewrite, which is great. Again, though, it requires ASP.NET, which not everyone may want.

IIS of course, has for many years had the ISAPI programming interface, which allows developers to build extensions and filters into IIS. So you could build a URL rewriter for IIS without ASP.NET. Again, most people don't want to get into that. They'd rather have something to configure.

There are a number of other rewriters available for IIS, but none that I found that were the right price, with the right features.

Ionic's ISAPI Rewriting Filter

Getting to the point, Ionic's ISAPI Rewriting Filter (IIRF) is a rewriting filter for IIS, along the lines of Apache's mod_rewrite. It is written in C, is relatively small and simple, does not require ASP.NET, supports regular expressions and RewriteCond attributes, and has a bunch of other nifty features. You can compile it yourself using a free compiler available from Microsoft, or using VS2005.

It works with all sorts of server-side technologies, including ASP.NET, but also with JSP, CFMX, PHP, Ruby, and frameworks built on same, including Joomla (Mambo) and others. It's easy to set up. It works on WinXP and WS2003.

IIRF is free and open-source, with a BSD license. I am in the process of setting up IIRF as a project on CodePlex .

Check it out.

-Dino