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
i added above code but i get 500 Internal server error.
ending tag is missing “/”, it should have been be
Just to clarify, in “Step 3” the closing tag of the “configuration > system.webServer > rewrite > rewriteMaps” node is missing a “/”
Thanks – have updated the closing tag now.
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 ;)