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

9,386 views

ruslany on May 19th 2010 in URLRewrite

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

21 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 250Kb (100Kb 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

  7. Gravatar Imagebryan responded on 22 Sep 2010 at 8:50 pm #

    Ruslany,

    I need to set up a ton of URL re-directs, We consumed an old web site but need to set up about 1000 re-directs. We have the mapping of old to new URL’s done, but we need to configure the rule or alias file. I do not know how to really do this effectivley. The Rules UI will not work for this many. Typically we did this in apache inside the httpd.conf file similar to a plain jane etc/hosts. It was very easy to maintain and didn’t require re-boots. Is there way to accomplish any easier in IIS 7.5 than installing the re-write module and input configuration to each URL to and from?

    Is there a simple list I can dump to map to?

    ‘Obi-rus’ your my only hope… :-)

    Thanks for your help in advanced!

  8. Gravatar ImageEdgar responded on 05 Oct 2010 at 10:33 am #

    Then, if I copy the htaccess file into the same path where web.config is, and modify , my mode rewrite used into my php application will work?

    Well, I really don’t know about how to activate the mode rewrite into a IIS server to use Apache use the mode rewrite for PHP applications

  9. Gravatar Imageruslany responded on 05 Oct 2010 at 11:46 pm #

    @bryan: This article may help.

    @Edgar: Have you tried importing the rules from .htaccess file as described here?

  10. Gravatar ImageBiker Man responded on 27 May 2011 at 5:34 am #

    Hi

    I am trying to set this up with iis7 and so I am adding in the line to the web.config as

    and then in the referenced file it look like this

    but the site errors. Is my structure incorrect on this as I am looking to make dynamic redirects and so this would be perfect if I could get is working

  11. Gravatar ImageJacob Coens responded on 31 May 2011 at 4:42 am #

    Hi Ruslany,

    I’m developing a ASP.NET User Control that provides a Web User Interface for managing a rewrite map. On my development environment, changes to the mappings file are picked up immediately by Url Rewrite, but on my test environment this does not happen until the application pool has been recycled. I’m guessing this is a caching-issue. Could you point me in the right direction? I think I should somehow clear the Url Rewrite cache after making modifications to the rewrite mappings file, but I can’t seem to find out how.

    In an earlier comment, you say: “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.”

    Could you explain how the IIS Manager UI for Url Rewrite does this?

    Thanks in advance!

  12. Gravatar Imageruslany responded on 06 Jun 2011 at 2:44 pm #

    Hi Jacob, I believe IIS Manager UI touches the corresponding web.config file when rules in rewritemap.config files are changed. This causes IIS to reload the configuration.

  13. Gravatar ImageJacob Coens responded on 10 Jun 2011 at 7:41 am #

    Ruslan,

    Thank you for your reply, so there was no magic coding involved on your behalf :-)
    I’ll let you know if it works.

  14. Gravatar ImageJacob Coens responded on 23 Jun 2011 at 5:48 am #

    Touching web.config works, although this means that I will have to make sure that the identity associated to the application pool has the appropriate rights to change the properties of the web.config file.
    By keeping the rewrite map in a separate file I hoped to have worked around this requirement, but unfortunately there is no way around it.
    Thanks again!

  15. Gravatar ImageTim Doscher responded on 23 Jun 2011 at 11:13 pm #

    Hi Ruslany,

    We are trying to get rewrite maps working on iis 7.5 and don’t seem to be having any luck. Do you know of any good steps for troubleshooting this feature? We are using the 2.0 url rewrite tool from microsoft.

    Thank you,
    Tim

  16. Gravatar Imageruslany responded on 24 Jun 2011 at 10:03 am #

    Hi Tim,

    This would be a place to start: Using rewrite maps. To troubleshoot the feature you can use Failed Request Tracing.

  17. Gravatar ImageJM responded on 08 Dec 2011 at 3:11 pm #

    I followed your examples (found on this page and those similar examples found on learn.IIS.net) and the rewrites worked with the rewrite map, except that my CSS wasn’t working. Once I added the following condition, it worked “”. Is this because my css file was being captured by the rule?

    Your articles are extremely helpful (I have used many as reference). Thanks again!

  18. Gravatar ImageJM responded on 08 Dec 2011 at 3:12 pm #

    Sorry, i tried to encode the xml…
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>

  19. Gravatar ImageAnkush responded on 29 Dec 2011 at 4:20 am #

    hi,
    i am doing the redirect using rewritemap.config concept which is acessed by web config , the size of rewritemap.config is around 3.3 mb but i am facing 500.52 error in the website instead if i reduce the size of rewritemap.config to 50 kb it works fine could please you tell me how to configure for file of size 3.3 ,mb i have allready tried changing registry value but it is not helping.

    thanks,
    ankush

  20. Gravatar ImageJames Moberg responded on 09 Jan 2012 at 8:27 am #

    Your sample web.config file has invalid XML.

    Change line #4 to:

  21. Gravatar ImageRajendra responded on 10 Jan 2012 at 11:34 pm #

    Hi Ruslany,
    We are using SDL tridion cms package for a new website.
    We have a requirement where the user can provide user friendly urls while creating a page in Tridion.
    Is there any built in functionality in tridion which can handle both the url and the internal links on page.
    Presently we have come up with two approaches
    1. using IIS url rewrite module and providing an external xml file(this file will be updated automatically when a user added a new page.)
    2. using httpmodule

    In both the cases whenever there is a change in the input file (for eg. if the user creates a new page, a new entry will be added to the xml file) the IIS or the application need to be restarted to reflect the changes.
    Is there any other way in which we dont need to restart the server?
    P.S. there is no fix pattern of url and there fore we cannot define regex rules.
    Thanks,
    Rajendra Dhaundiyal

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