Redirecting Old Pages with IIS 7 and the URL Rewrite Module

I am a change-a-holic.  I am constantly changing, re-factoring, deleting, moving, renaming, etc. all in the name of progress.  It’s intentional, of course, and I learned to embrace change when I started following the principles of Extreme Programming like Refactor Mercilessly.

An elegant way to handle change when it comes to renaming and moving pages around within an ASP.NET web application is to take advantage of the IIS 7 Rewrite module.  With this strategy in place I can easily add redirects to a single xml file and update it as needed.

Here’s how to get it setup:

  • Make sure you have the URL Rewrite module installed in IIS 7.
  • Add a file called Web.RewriteMaps.config to your web application root.  This is the file that will contain all your redirect mappings.  It should look like this:
  • <rewriteMaps>
      <rewriteMap name="OldPages">
        <add key="/oldpage.aspx" value="/newstuff/newpage.aspx" />
        <add key="/legacy/really_old.aspx" value="/BrandNew.aspx" />
      </rewriteMap>
    </rewriteMaps>
  • Add the following to the <system.webServer/> section of your web.config file.  If you already have a <rewrite/> section don’t duplicate but merge the following in.
        <rewrite>
          <rewriteMaps configSource="Web.RewriteMaps.config"/>
          <rules>
            <rule name="Old Page Redirects" stopProcessing="true">
              <match url=".*"/>
              <conditions>
                <add input="{OldPages:{REQUEST_URI}}" pattern="(.+)"/>
              </conditions>
              <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
            </rule>
          </rules>
        </rewrite>
  • That’s it.  Now IIS will read you Web.RewriteMaps.config file and perform a ”301 Moved Permanently” on all your old pages and redirect the user to the new page.

2 Comments

  1. Phil Smith says:

    This is a fantastic article! I have to clean a client’s site page names but their site is currently built on a PHP CMS with any comments in French! This was the final piece in the puzzle to allow me to move the site to ASP.NET! Many thanks!

  2. Mohan says:

    This is just great. Helped me solve a long time problem.

Leave a Reply