Apache mod_rewrite for IIS (IIRF)


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

Comments (6)

  1. Wow. I built one of these, and think I even released the source, several years ago.

    Anyways, I abandoned it when I heard initial info about IIS 6, especially that it would require a switch to a lower performance compatibility mode if you wanted to use ISAPI filters. Is this not true?

  2. You’ve been kicked (a good thing) – Trackback from DotNetKicks.com

  3. cheeso says:

    Dennis – No, not true.  The use of an ISAPI filter within IIS6 does not imply that you must use IIS5 compatibility mode.

    Here’s a good blog post on that topic by David Wang:

    http://blogs.msdn.com/david.wang/archive/2006/06/21/ISAPI_Filter_and_the_IIS6_Process_Model.aspx

  4. Steven says:

    I programmed two isapi filters, one for url rewriting and the other one for authentication (db, text), both are free and open source. You can find them at http://www.iismods.com.

    If you like them, help me spread the word 😉

    Cheers

  5. [来源:All About Interop] Apache有个mod_rewrite 能根据文本文件里的设置来重写URL。奇怪的是这么多年,IIS却没有这样的东西,可见我们的依赖性之强,。虽然IIS6

  6. A couple weeks ago, Bill Staples made a splash when he announced a Rewrite Module for IIS7: http://blogs.iis.net/bills/archive/2008/05/31/urlrewrite-module-for-iis7.aspx