Search code examples
http-redirectiisweb-configiis-8windows-server

Best practice for maintaining lists of redirect urls with IIS 8?


Some website changes caused redirect lists with 400+ URLs. What is the best way to maintain these? Adding them one by one is not a good option. Adding them to the the web-config causes the file to grow to the point of the system being unable to read it.

Is there a way to maintain a separate file with redirects in IIS 8?


Solution

  • From my experience best way is to use rewrite maps:

    Create rewriteMaps.config

    <rewriteMaps>
        <rewriteMap name="Redirects">
            <add key="/test.aspx" value="/test2.aspx" />
            <add key="/aboutus.aspx" value="/about" />
        </rewriteMap>
    </rewriteMaps>
    

    Create rewriteRules.config

    <rules>
        <rule name="Rule for Redirects">
            <match url=".*" />
            <conditions>
            <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
            </conditions>
            <action type="Redirect" url="{C:1}" appendQueryString="false" />
        </rule>  
    </rules>
    

    In web.config use configSource attribute

    <rewrite>
        <rewriteMaps configSource="rewriteMaps.config" />
        <rules configSource="rewriteRules.config" />
    </rewrite>
    

    Benefits of this structure

    • Web.config small and clean
    • You can put complex redirects/rewrites with conditions in rewriteRules.config
    • You can put simple redirects (redirect pathA to pathB) directly into rewriteMaps.config as key-value pair