Storing URL Re-Write rules in a seperate file

When using rewrite maps in IIS URL Rewrite it is very common to have a very large number of entries in a rewrite rules. In order to avoid cluttering the configuration file – web.config – with this configuration data the rewrite rules can be defined in a separate configuration file. That file can then be referenced from the web.config file.

For example below are few of the rules defined in one of my web application:

<system.webServer>
      <rewrite>
            <rules>
                 <rule name="HomeURL" stopProcessing="true">
                    <match url="^Home$" />
                    <conditions logicalGrouping="MatchAll"
                      trackAllCaptures="false" />
                     <action type="Rewrite" url="Pages/HomePage.aspx"
                      appendQueryString="false" logRewrittenUrl="true" />
                  </rule>
                  <rule name="DefaultURL" stopProcessing="true">
                     <match url="default" />
                     <conditions logicalGrouping="MatchAll"
                        trackAllCaptures="false" />
                     <action type="Rewrite" url="Pages/HomePage.aspx"
                        appendQueryString="false" logRewrittenUrl="true" />
                  </rule>
               </rules>            
      </rewrite>
  </system.webServer>

Create a seperate file called myRules.config in the same directory where web.config file is and move the rules to the new file:

		<rules>
                       <rule name="HomeURL" stopProcessing="true">
                             <match url="^Home$" />
                             <conditions logicalGrouping="MatchAll"
                                  trackAllCaptures="false" />
                             <action type="Rewrite" url="Pages/HomePage.aspx"
                                  appendQueryString="false" logRewrittenUrl="true" />
                        </rule>
                        <rule name="DefaultURL" stopProcessing="true">
                             <match url="default" />
                             <conditions logicalGrouping="MatchAll"
                                 trackAllCaptures="false" />
                             <action type="Rewrite" url="Pages/HomePage.aspx"
                                 appendQueryString="false" logRewrittenUrl="true" />
                        </rule>
               </rules>    

Save the new file and now open web.config file in notepad. In web.config file add the following inside the <rewrite> section:   

<system.webServer>
      <rewrite>
         <rules configSource="myRewriteMaps.config" />
      </rewrite>
 </system.webServer>

The configSource attribute tells IIS configuration that the <rewriteMaps> section is defined in a separate file myRules.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 myRules.config file.

The same approach can be used for storing rewrite mappings in seperate configuration file. For Ex:

<system.webServer>
      <rewrite>
          <rules configSource="myRewriteMaps.config" />

</rewrite>

</system.webServer>

Hope this helps!!! 🙂

Umesh Mehra

Lead Engineer, Global Logic, Noida, India

More Posts