IIS.NET uses URL rewrite module

IIS.NET team has been very proactive in helping us out with testing of URL rewrite module. In fact, they even agreed to deploy the latest build of the module on the production server that hosts http://www.iis.net. This kind of real-life deployments really helps us validate the features and functionality of the module. And being able to do this validation so early in release cycle gives us a good opportunity to adjust the feature set, re-consider some of the design decisions, or just find some very good bugs.

IIS.NET has URL rewriting requirements, which are typical for large content management systems. The articles on the site are often moved or updated and the old links should continue to work. Or there is a need to have a nice URL (for example – http://www.iis.net/fastcgi) for a page that currently has some meaningless URL (e.g. http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1521). IIS.NET has more than a hundred of such URL mappings. Majority of those mappings are static, so there is no need to use regular expression patterns, capture groups, back-references or any other advanced URL rewriting stuff. Rewriting logic is very simple:- for example if input URL is “/php” then rewrite it to “/default.aspx?tabid=50001″. But if we tried to define this logic by creating a rewrite rule per URL mapping we would end up with more than a hundred of rules, which would be evaluated for every request. That would kill the performance of the web site.

The better approach for implementing this kind of rewriting is to use the rewrite map concept in URL rewrite module. Rewrite map can be used to define a set of key/value pairs and then use them within a single rewrite rule. For example each key/value pair can describe a combination of “nice” public URL and corresponding “not-so-nice” internal URL. A rewrite rule can reference a rewrite map and pass the “nice” URL as a key to rewrite map. The rewrite map will lookup this key and will return corresponding value, which would be the “not-so-nice” internal URL. Then the rewrite rule will use that URL for rewriting.

Here is an example of how this works for IIS.NET. First, we define a rewrite map like this:

<rewriteMaps>
  <rewriteMap name="IISNETRewrites">
    <add key="" value="default.aspx?tabid=1" />
    <add key="getstarted" value="default.aspx?tabid=2" />
    <add key="team" value="default.aspx?tabid=6" />
  </rewriteMap>
</rewriteMaps>

Then we define a rewrite rule that references this map and passes the requested URL as a key to this map:

<rules>
  <rule name="MainRewrite" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="{IISNETRewrites:{R:1}}"/>
  </rule>
</rules>

So, when browser makes a request to http://www.iis.net/getstarted, the rewrite rule pattern “(.*)” captures “getstarted” in a back-reference “{R:1}” which is then passed as a lookup key to the rewrite map “{IISNETRewrites:{R:1}}”. The rewrite map looks it up and returns “default.aspx?tabid=2″ which then is used by rewrite rule for rewrite action. As a result, the URL is rewritten to http://www.iis.net/default.aspx?tabid=2.

Check out some of the URL’s that are rewritten by URL rewrite module:

If you are trying URL rewrite module, make sure to check out the rewrite maps functionality and let us know if you have any comments or suggestions.

19,151 views

ruslany on July 18th 2008 in URLRewrite

PoorFairAverageGoodExcellent (5 votes, average: 5.00 out of 5)

3 Responses to “IIS.NET uses URL rewrite module”

  1. Gravatar ImageBill Staples responded on 18 Jul 2008 at 11:51 pm #

    Great blog entry, and slick looking blog! Are you also running your own server? :)

  2. Gravatar ImageJoe Fiorini responded on 21 Jul 2008 at 8:35 am #

    Hey Ruslan, thanks for all your help getting the Rewrite module implemented! It’s an excellent product that’s a long-time coming for IIS! I’m very excited to continue using it.

  3. Gravatar Imageruslany responded on 21 Jul 2008 at 1:14 pm #

    Of course I am running my own server :) – Windows Web Server 2008.

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles