Using Rewrite Maps in IIS for 301 Redirects

The following steps out line how to set up the redirects using rewrite maps in IIS.

These steps are based on the following blog post and articles from Ruslan Yakushev:

Note: you need to be using IIS7 or above, with ASP.NET role service enabled, AND the URL Rewrite Module installed.

Step 1: Create the rewrite mapping file

This is a text file, saved with the name rewritemaps.config, here’s the example format:

    <rewriteMap name="Redirects">
        <add key="/oldurl" value="/newurl" />
        <add key="/oldurl2" value="/newurl2" />

Step 2: Copy to IIS

Copy the rewritemaps.config file into the same directory as your web.config (on IIS)

Step 3: Add a reference to web.config

Add a reference to the rewritemaps.config file in your web.config file eg:

            <rewriteMaps configSource="rewritemaps.config"></rewriteMaps>
                <rule name="Redirect rule1 for Redirects">
                <match url=".*" />
                    <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
                <action type="Redirect" url="{C:1}" appendQueryString="false" />

Step 4: Load into IIS

Simply make a change to the web.config file and save – this will force the web.config to be reloaded in IIS (and thus for the rewritemaps.config file to be loaded).

You can also recycle the IIS application pool so that the new web.config details are loaded.
Or, you can modify the rewrite rules using the IIS Manager UI and this will reload them (‘touching’ the mapping file via the UI causes it to be reloaded).

Step 5: Increase IIS rewritemaps.config size limit (Optional)

If you have extremely large rewritemaps files (eg with thousands of redirects) they may be larger than the default file size of 256KB.

In this case you will need to set a registry setting to increase the default size allowed. This setting affects web.config and all other .config files (ie the rewritemaps.config files).

Set to: 2048

Note: this requires a server reboot!

For further details see this support knowledge base article:

Note: if you are running 32 bit application pool, you’ll need to use a different reg key, details are available here:

5 thoughts on “Using Rewrite Maps in IIS for 301 Redirects

  1. Craig says:

    Hey Craig, many thanks for this – Just want to make sure I have all my i’s dotted and t’s crossed before I apply the solution. I’m more accustomed to .htaccess for such things, so here goes:

    So in the mapping file I’ve changed the following: line 2

    I also changed the ‘key’ entries to apply to my specific use case.

    In the web.config reference, I’ve replaced the following: line 4

    Along with: line 6

    It looks like that tallies up in my eyes but, as I say there’ll be a lot of eyes on this even in test environment, so I don’t want it stalling things.

    Much appreciated on the guide and I’ll promo it far and wide ‘when’ it works for me ;)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.