Redirecting specific URLs using Rewrite Maps

If you have ever managed a website for a period of time you know that business needs change requiring website structure changes.  You may have old links that you need to preserve but want the user to go to the new URL.  That’s a great job for URL Rewrite and Rewrite Maps.  Let me show you how.  If you would like to skip doing this through the GUI, you can scroll to the bottom and see what to add to your web.config although I recommend reading through the process to understand how I came up with that.

Open up IIS Manager on your server.  Select your website in the left pane and double click the URL Rewrite icon in Features View to bring up the URL Rewrite view.

In the URL Rewrite view, select View Rewrite Maps… from the Actions pane on the left:

This brings up the URL Rewrite Maps view.  Click Add Rewrite map…

Give it a meaningful name.

You are now at the Edit Rewrite Map screen.  In the Actions pane select Add Mapping Entry…

This brings us to the meat of the rewriting.  Here is where you specify the original value and the new value.  In this example, let’s redirect from http://www.yourdomain.com/test.aspx to http://www.yourdomain.com/test2.aspx.  The rewrite rule that we will create shortly will take care of the bulk of the work.  We just need to worry about test.aspx and test2.aspx, so let’s add those, making sure to not forget the / before test.aspx.

Click OK and select Back to Rules in the Actions pane.

Now we’re back at the URL Rewrite screen.  In the Actions pane select Add Rule(s)…

This brings up an Add Rule(s) window.  Select Rule with rewrite map and click OK.

We are presented with another window to Add Rule with Rewrite Map.  We have two options for action, Rewrite or Redirect.  In most cases I choose Redirect, which does a 301 redirect (by default) to the new page.  You may have a reason to want to do a Rewrite instead.  Next select the rewrite map that we created above and click OK.

At this point you are all set and http://www.yourdomain.com/test.aspx will redirect to http://www.yourdomain.com/test2.aspx.  You can go back to the Rewrite Maps and add more specific URL redirects.  You can also edit this URL Rewrite rule and change it from Redirect to Rewrite, make it a temporary 302 redirect instead of a 301 redirect and much more.

If you don’t want to bother with the GUI or if you don’t have access to IIS manager, you can add the following to your web.config instead.

<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name=”Test Rewrite Map”>
<add key=”/test.aspx” value=”test2.aspx” />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name=”Redirect rule1 for Test Rewrite Map”>
<match url=”.*” />
<conditions>
<add input=”{Test Rewrite Map:{REQUEST_URI}}” pattern=”(.+)” />
</conditions>
<action type=”Redirect” url=”{C:1}” appendQueryString=”false” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Rick is a Senior Support Lead at OrcsWeb, a hosted server company providing managed hosting solutions.