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.

4 thoughts on “IIS.NET uses URL rewrite module”

  1. 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.

  2. Hey Ruslany,

    can i specify 2 configsource files in my web.config
    <system.webServer>
    <rewrite>
    <!–<rules>–>
    <rules configSource="Configs\rewriteRulesUK.config" />
    <!–</rules>–>
    </rewrite>
    </system.webServer>
    <location path="Configs/France">
    <system.webServer>
    <rewrite>
    <!–<rules>–>
    <rules configSource="Configs\France\rewriteRulesFR.config" />
    <!–</rules>–>
    </rewrite>
    </system.webServer>
    </location>

    the rewriteRulesUK and FR config contains the redirect rules

    rules in uk.config

    <rules>
    <rule name="content" stopProcessing="true">
    <match url="content.*" />
    <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="www.abc.com" />
    </conditions>
    <action type="Redirect" appendQueryString="false" url="http://www.xyz.com/&quot; redirectType="Permanent" />
    </rule>
    </rules>

    rules in france.config

    <rules>
    <rule name="fr" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="www.abc.com" />
    </conditions>
    <action type="Redirect" appendQueryString="false" url="http://www.xyz.com/fr/&quot; redirectType="Permanent" />
    </rule>
    </rules>

    How can i do this?

    these are only for 2 countries but i need to do this for around 10 countries and each has some what more than 250 old url which are to be redirected to new url.

    right now i using a different concept for this. but i need to know if this is possible if yes, how?

    Thanks,
    MD

Leave a Reply

Your email address will not be published. Required fields are marked *