Storing URL rewrite mappings in a separate file

When using rewrite maps in IIS URL Rewrite it is very common to have a very large number of entries in a rewrite map. In order to avoid cluttering the configuration file - web.config - with this configuration data the rewrite maps can be defined in a separate configuration file. That file can then be referenced from the web.config file. This post provides an example of how this can be done.

Create a file called rewritemaps.config in the same directory where web.config file is. Open that file in notepad and add the following:

<rewriteMaps>
  <rewriteMap name="Redirects">
    <add key="/oldurl" value="/newurl" />
    <add key="/otheroldurl" value="/othernewurl" />
  </rewriteMap>
</rewriteMaps>

Save this file and then open web.config file in notepad. In the web.config file add the following inside of the <rewrite> section:

<rewriteMaps configSource="rewritemaps.config" />

The configSource attribute tells IIS configuration that the <rewriteMaps> section is defined in a separate file rewritemaps.config. This referenced section can be now uses as if it was defined in the same web.config file. Also, the IIS Manager UI will work well with this referenced file: when you modify or add entries to the rewrite map they will be stored in the rewritemaps.config file.

Here is a complete example web.config file that uses the rewrite map from referenced configuration file:

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

The same approach can be used for storing rewrite rules in a separate configuration file, e.g.:

<rules configSource="rewriteRules.config" />

1,664 views

ruslany on May 19th 2010 in URLRewrite

PoorFairAverageGoodExcellent (2 votes, average: 5.00 out of 5)

6 Responses to “Storing URL rewrite mappings in a separate file”

  1. Gravatar ImageAJ responded on 21 May 2010 at 12:43 pm #

    Does this also apply to applicationHost.config rules for server level rewrites?

  2. Gravatar Imageruslany responded on 21 May 2010 at 4:03 pm #

    This does not apply to applicationHost.config. It only works with web.config files.

    Also, note that if you manually edit rewritemaps.config file by using a text editor then the changes to the file will not take effect until you recycle the IIS application pool. But if you modify the rewrite map entries by using IIS Manager UI then the changes will take effect right away.

  3. Gravatar Imageruslany responded on 04 Jun 2010 at 4:18 pm #

    Useful information: the maximum allowed size of web.config file by default is 250Mb (100Mb in Vista).

    The default can be changed by setting a registry key HKLM\SOFTWARE\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB (REG_DWORD)

    More information on this and other registry keys can be found at List of registry keys affecting IIS7 behavior

  4. Gravatar Imager4ds responded on 29 Jun 2010 at 10:50 am #

    Is it possible to have a different set of URL rewrite rules on the same domain? For example website.com might have one set of rules, while domain.website.com might have another or add to the already existing rules?

  5. Gravatar ImageFB responded on 13 Jul 2010 at 4:11 am #

    can we have the 301 redirects stored as a separate file too? if yes, how?

    thanks

  6. Gravatar Imagenk responded on 23 Jul 2010 at 12:02 am #

    hi
    this is my url link
    herbal.aspx?link=rejuvenating butea.txt&id=8&h=butea

    - i want like this

    herbal/butea/rejuvenating butea
    can you pl guide me to do. i m very new to iis 7

    thank you
    nk

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles