We already know the importance and usefulness of url rewriting for websites. With IIS 7, Microsoft introduced URLRewrite module which enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find. This module is not a part of OS but it can be downloaded separately. IIS7 is written so smartly that it leaves minimum footprint of memory and hence is less exposed to threats. We enable only those features which are needed for our websites to work. This was possible because most of the features are built as modules. Apart from the built in modules like Authentication, Tracing, ASP.NET etc, there are out of band modules available which are not clubbed with operating system and are available for download IIS website. URLRewrite is one such out of band module available for IIS 7 onwards and because of provided GUI, this makes url rewriting pretty easy.
URLRewrite is written as a native module for IIS 7 and you can find it in IIS by looking at the modules section.
But, we still have a lot of customers who are using IIS 6. (IIS 6 is clubbed with Windows Server 2003) and we do not have URL rewriting available with IIS6 as a built in feature.
As we already know that a module intercepts the request on IIS and causes the change in the behavior of the request, so does RewriteModule. If you have set up any rule in IIS 7, this module will rewrite or redirect the request accordingly.
Now the question is how do we achieve the same thing in IIS 6. As we already see that in IIS 7, a module(URLRewrite) is doing the job for us. We can follow the same approach in IIS6 as well. So we have got two options.
1. Write a .NET HttpModule and register it in web.config file of the application. (What if your website does not have asp.net content or this is just simple html , php website?)
2. You can write your own native module and register this is IIS 6. (Considering the efforts required, i would not go with it.)
So here, i am writing about one such already available native module IIRF which is a free and open-source project hosted on codeplex. Important thing is that it is free of cost and if you are really passionate and want to contribute for this, you can do that as well by adding features and fixing bugs in it.
What is IIRF?
IIRF is Ionic’s ISAPI Rewriting Filter, a free and open source rewriting filter for IIS. The source as well as binary packages are available on the IIRF codeplex project page. Currently its 2.1 version is available to download.
How to enable in IIS?
1. You can use the MSI package to install IIRF through the Windows Installer system. Or,
2. You can install IIRF "manually".
Since first option is pretty simple, we will look at the second manual option. Here are the steps to manually configure it.
A. Here is the downloaded content. We will only need IIRF.dll
B. I copied IIRF.dll at this path C:\Inetpub\wwwroot\IIRF. I deliberately copied the dll inside wwwroot folder, however, you can copy it anywhere. In that case we will have to give Default permission to this dll following this document. http://support.microsoft.com/kb/812614
C. Open the website and select ISAPI Filters, add filter, give any name and path of this dll
d. You should see one entry like this and we are done with the configuration of ISAPI filter. Next steps is to create rules.
Creating rewriting rules
I have intentionaly copied the dll inside the content folder of the website because I want to configure this rule only for one website. We can apply the same filter for multiple websites as well by selecting the “Websites” node in IIS and right click –> Properties –> ISAPI filters
IIRF rewrite filter reads its configuration from an .INI file and we will have to create this file. This .INI file will actually contain our rewrite/redirect rules and you can consider it as a web.config or applicationHost.config file for IIRF module.
Now there are two types of .INI config files. one is global and applicable to all the websites and one is local to the application. Adding rules in both the files have same format.
Settings inside this file is applicable to all the websites. We put IirfGlobal.ini in the same directory where IIRF.dll exists. If this file does not exists at all, IIRF filter will continue.
This is your configuration file specific to the website. If we have virtual directories or further applications inside the main website, we can make a copy of this file at each level to override the settings in parent directory.
Here we will look at a sample file IIRF.ini
RewriteEngine ON is the first thing, that you will need for your rewriting to work. If you switch it off with RewriteEngine OFF, any of the added rules will work.
On top of the iirf.ini, you will find some license stuff, please do not remove it for legal reasons. This module is anyways free. Here I have removed that part to keep it simple and make you understand this stuff easily.
Here I have added one simple rule which rewrites all the incoming urls to bing.jpg
RewriteRule ^/.*$ /bing.jpg
Here is what i get in the response.
If we want to use redirect, we can do this using RedirectRule and here is one such sample.
RedirectRule ^/(iisstart.*)$ http://bing.com
Above rule will redirect all incoming requests which starts with iisstart to http://bing.com
Logging feature is also available in this module. If any of your rule is not working or for any purpose, you can also use logging feature for troubleshooting.
RedirectRule ^/(iisstart.*)$ http://bing.com
A log will be generated at this path C:\temp with the naming convention namegiven.ProcessIdW3wp.Log
You have almost every feature in this module which is provided in IIS 7 URLRewrite module like support for Regular Expression, RewriteCont it. It works well with ASP.NET, PHP Ruby, JSP etc.
A very good documentation is available here http://dotnetzip.herobo.com/Iirf21Help/frames.htm and you can find so many available options in this module.
IIRF and its documentation is distributed under the Microsoft Permissive License (Ms-PL). Please go through following link for more information on this. http://iirf.codeplex.com/license
Supportability: Since this is an open source module on codeplex.com, Microsoft CSS will not provide any kind of support for this module. However, you can go on this link http://iirf.codeplex.com/discussions to raise your concerns.
Please feel free to write the feedback if it helps.