Using URLRewrite in IIS

Using URLRewrite in IIS

Using URLRewrite in IIS

1. Introduction

URL Rewrite is IIS module that enables Web administrators to create rules to modify URLs on the fly, transparently. This modified URL may be more convenient and easier for users to remember. You can use URL Rewrite to change the URL used in your IntraWeb ISAPI application.

2. Pre-requisites

2.1. Internet Information Services (IIS)

2.2. URLRewrite 2.0 Module. This extension may be downloaded here: http://www.iis.net/downloads/microsoft/url-rewrite

3. Installing the pre-requisites on your Windows Server

Follow the link on item 2.2, download and execute the URL Rewrite 2.0 Module installer. It will then launch the Web Platform Installer. The installation is pretty simple and straightforward.

When the installation finishes, Web Platform Installer gives you a list of other products to install. You don’t need to install anything else. Just close the installer.

Once the installation is complete, you will find a new feature named “URL Rewrite” in IIS. You will find the same feature in each of your Virtual Applications:

4. Creating Rewrite Rules

Writing rewrite rules for URL Rewrite module is not simple as it could be. I will not enter in much detail here because it is a really broad and complex subject. You can find detailed information about URL Rewrite rules here: http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

You can write your rules using IIS console, or editing your web.config file directly. I find it easier to edit the web.config file, so let’s start with a sample web.config:

<?xml version=”1.0″ encoding=”UTF-8″?><configuration> <system.webServer> <handlers accessPolicy=”Read, Execute, Script” /> <rewrite> <rules> <rule name=”Rewrite rule1 for Intraweb” stopProcessing=”true”> <match url=”Features/(.*)” /> <action type=”Rewrite” url=”URLRewrite/FeaturesISAPI.dll/{R:1}” logRewrittenUrl=”true” /> </rule> </rules> </rewrite> </system.webServer></configuration>

All the rewrite rule logic is contained within the <rewrite> tag. We named it “Rewrite rule1 for Intraweb”. In this case we are using the FeaturesISAPI.dll application. This rule is basically telling IIS to:

Replace “Features/” in any incoming request with “URLRewrite/FeaturesISAPI.dll”

In this case, URLRewrite is the name of your virtual application, and FeaturesISAPI.dll is the name of your ISAPI DLL. Using this web.config sample is quite simple to create a rule for your DLL, isn’t it?

This sample web.config file must be saved in the same directory of your Virtual Application.

Once the web.config file is saved to your virtual application root directory, you can see the rule in IIS console (double-click URL Rewrite icon in the right panel, after selecting your virtual application):

If you double-click the rule, you can see its details and also modify it:

5. Application Setup

An IntraWeb ISAPI application does not know if you are using or not a URL Rewrite rule in IIS. You have to instruct your application to use the same logic used by URL Rewrite, in a reverse way. This is done in ServerController.OnConfig() event:

procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);begin Self.RewriteURL := ‘/Features’;end;This corresponds to the <match url=”Features/(.*)” /> part of the URL Rewrite rule defined in web.config file, but there must be a slash before. Rebuild your application and you are done. Of course, you can load this property from the web.config or even an .ini file and set it at runtime.

6. Testing your application

After your setup is ready, you can now test your ISAPI application using the URL Rewrite module. Note that we reference this application using this URL:

http://localhost/Features/

URL Rewrite module will change this to http://localhost/URLRewrite/FeaturesISAPI.dll and the application works as expected, transparently. Note that the name of the ISAPI dll is not part of the URL anymore:

7. See also

  • Deploying your application as a ISAPI