IIS Tutorial Series (Part 3): URL Rewrite on IIS

August 9th, 2010 | Tags: , ,

Note: This article is Part 3 of the IIS Tutorial Series.

URL Rewriting is a powerful functionality that provides you the ability to modify your URL’s appearance. If you’re reading this, you probably know what a rewritten URL is so I won’t go into examples. URL Rewrite has became so common due to its benefits of the relevance of the URL to its page content for Search Engine Optimization (SEO) and for link sharing (e.g. social media), many Content Management Systems (CMS), such as WordPress, and programming frameworks already provide out-of-the-box ability to transform ugly URLs to “pretty URLs”. That is, on the front-end. However, the web server cannot understand the pretty URLs to send the results properly back to the web application. That is what a URL Rewrite module is for. It converts transparently the pretty URLs back to the ugly URL so the web server can send the data properly back to the application.

For those who’ve worked with Apache, almost all types of modules for your needs are readily available, easily to download if necessary, and a plentiful of documentation available. Fortunately, for IIS 7.0 or later users, Microsoft does provide a URL Rewrite Module (which I won’t go into here). Unfortunately, for users of IIS 6.0, we have to resort to third-party modules to provide URL Rewrite functionality. Although there are a few of them that are easy to find, most of them are not free, or they provide a free limited version, such as only a limited number of URL rewrites per day, or rewrite rules that are active for the whole server, and not for individual websites. In this tutorial, I’ll introduce a free and powerful URL Rewriter, and step through the installation and basic setup.

Download and Installation
Iconics ISAPI Rewrite Filter (IIRF)
is a free ISAPI Rewrite filter that works on IIS 6.0 or later. It can be set up to be used similar to Apache’s .htaccess rules so that rules applies only to a specific website, or even a single directory. The rewrite rules are also very similar to, if not exact, Apache’s rewrite rules. To begin, visit the IIRF website and download the ZIP file (version 2.0, at this time of writing).

Installing is as simple as unzipping the file, and placing the folder in the root of your server’s hard drive (e.g. C:\IIRF\). That’s it! IIRF is installed!

Website Setup
To set up an IIS website to use IIRF, open up your IIS Manager, select your website, right-click and select “Properties”. Click on the ISAPI Filters Tab at the top.

Website Filter Properties

On the left, click on “Add…” to add a new filter. Use a name that is easy for you to understand. Here, I used “ISAPI Rewrite” as the Filter Name. For the Executable, browse to the DLL of your IIRF installation. It should be in the “bin” folder (e.g. C:\IIRF\bin\IIRF.dll).

Add Filter Properties

Hit “OK” to save your settings, and “OK” again to save and exit properties. Just like that, your website should be enabled to use IIRF. You’ll have to do this for each website on the server that you want to use URL Rewrite for.

Adding Rewrite Rules
This is very similar to Apache’s .htaccess rules. The rules for IIRF are so similar, pretty much any rules that I find online for .htaccess I can use for IIRF, and it works fine. To add rules, create a file called IIRF.ini in the directory you want to use the rewrite on. For most of us, that should be the root public directory of the website.

To do a quick check on whether IIRF is set up properly, edit the IIRF.ini file and add the following line:

StatusUrl /iirfStatus RemoteOk

This will load a status page at the /iirfStatus directory of your URL. You’ll also need to be sure you have a host name or URL mapped to your website to test this. For example, if your website is at test.com, visit http://test.com/iirfStatus to see the status of IIRF. If a status page loads that is similar to the image below, IIRF is working properly for the website. (You’ll probably want to remove this StatusURL line before production.)

IIRF Status ReportThat’s it! URL Rewrite should be enabled for your website. For a list of rules and logging options, there should be a help guide in the IIRF installation folder: /AdminGuide/Help/IirfGuide.chm
An online documentation version is also available. If you need help, the discussion forum is also fairly active and useful.

No comments yet.