Using Rewrite Maps in IIS for 301 Redirects

Setting up redirects in IIS

(Note: The content in this post was originally in our Resources section, but I’m re-posting it here to make it easier to find.)

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:

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

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:

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

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

HKLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB
(REG_DWORD)
Set to: 2048

Note: this requires a server reboot!

For further details see this support knowledge base article:
http://support.microsoft.com/kb/954864

Note: if you are running 32 bit application pool, you’ll need to use a different reg key, details are available here:
http://forums.iis.net/p/1196321/2044580.aspx/1

Craig Bailey

Craig is the Founder and Technical Director at XEN Systems. He’s been involved in IT, web development and digital marketing for more than 25 years.

One thought on “Using Rewrite Maps in IIS for 301 Redirects

  1. Hugo says:

    Hi! Since I still got some problems with my rewrite map on an iis server, I wondered where I can get professional help. I tried to handle it on my own, but without success.
    Maybe you have some recommendation for agencies or programmer / freelancer, who could help 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.