Dela via


URLRewrite in IIS6 - IIRF

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.

 image

 

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 

 

image

 

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. https://support.microsoft.com/kb/812614

 

C. Open the website and select ISAPI Filters, add filter, give any name and path of this dll

image

 

d. You should see one entry like this and we are done with the configuration of ISAPI filter. Next steps is to create rules.

 

image

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.

 

1. IirfGlobal.ini

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.

2. IIRF.ini

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.

 

RewriteRule

Here I have added one simple rule which rewrites all the incoming urls to bing.jpg

image

 

RewriteEngine ON

RewriteRule ^/.*$ /bing.jpg

 

Here is what i get in the response.

 

image

 

 

RedirectRule

If we want to use redirect, we can do this using RedirectRule and here is one such sample.

 

RewriteEngine ON

RedirectRule ^/(iisstart.*)$   https://bing.com

 

Above rule will redirect all incoming requests which starts with iisstart to https://bing.com

Logging

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. 

 

RewriteEngine ON

RewriteLog C:\temp\iirf

RedirectRule ^/(iisstart.*)$   https://bing.com

 

A log will be generated at this path C:\temp with the naming convention namegiven.ProcessIdW3wp.Log

image

 

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 https://dotnetzip.herobo.com/Iirf21Help/frames.htm and you can find so many available options in this module.

 

Disclaimer:

IIRF and its documentation is distributed under the Microsoft Permissive License (Ms-PL). Please go through following link for more information on this. https://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 https://iirf.codeplex.com/discussions to raise your concerns.

 

Please feel free to write the feedback if it helps.

--

Thanks

Gaurav

Comments

  • Anonymous
    January 11, 2016
    The comment has been removed
  • Anonymous
    March 10, 2016
    After adding ISAPI filter, no guidelines on where to save the ini file and how to make ISAPI filter to use the INI file..I am looking for an HTTP redirection from one site to another site at individual page level.
    • Anonymous
      April 12, 2016
      @SivaThere are two types of files, Global and website specific.1. IIRFGlobal.INI: We put IirfGlobal.ini in the same directory where IIRF.dll exists.2. IIRF.ini: 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. [Basically this means, INI files needs to be copied in application directory.]Let's say you have folder structureMainFolderMainFolder/SubFolderNow you can have same file copied at both locations. File inside subfolder will override rules in MainFolder.I hope this clears it.ThanksGaurav
  • Anonymous
    March 30, 2016
    @SteveURLRewrite with Cassini does not work. But in IIS, you can do that with Visual Studio. You can configure your website in IIS and debug that in Visual Studio. http://weblogs.asp.net/owscott/visual-studio-intellisense-for-url-rewriteIn IIS 7 onwards, there is a separate module to configure URL Rewrite rules. Please refer here http://www.iis.net/downloads/microsoft/url-rewriteHope this helps.ThanksGaurav
  • Anonymous
    November 07, 2016
    After a whole afternon trying to install IIRF on my windows server 2008/IIS6.1 (but looks like IIS 7 interface), with the .msi, then manually, giving all the necessary permissions, impossible to make it work. With the .msi, always error 500 (and impossible to install in an other location than "program files").Manually, always error 404.I put all the ini files where they have to be, IIRF appears in the isapi filters of the web site in IIS. I'm soooo desapointed.Shall I have to spend a hundred an twenty euros with Helicon isapi rewrite 3 (cause I have several web sites) while your isapi would do the job ?I know that there is no support, but I don't know what else to do... :-(
  • Anonymous
    November 12, 2016
    How to simply rewrite HTTP to HTTPS for my entire site
    • Anonymous
      November 19, 2016
      Please refer to this documenthttps://blogs.msdn.microsoft.com/kaushal/2013/05/22/http-to-https-redirects-on-iis-7-x-and-higher/ThanksGaurav