Talk with an Advisor
Talk with an Advisor

1 min read

Using Rewrite Maps in IIS for 301 Redirects

(Note: Here's why redirects are so important)

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

Interesting Reading This Week

If you follow us on Facebook, Twitter or Google+ then you've likely already seen us linking to the following posts. But if not, here's a quick review...

Read More

Important Information For Your Website: [insert domain] [unique characters]

You probably receive emails like the following. I know I do, probably a few each day...

Read More

Building a Twitter Following with Follower Wonk

Building a Twitter following takes time and a certain amount of determination. Whilst some companies stick to tried and tested solutions, others are...

Read More