Search code examples
asp.netiisumbraco

ASP.NET web.config specific redirects with a 'catch all'


I wonder if anyone could shed any light on something that I don't think is possible.....

A site is being decommissioned and the site owners have supplied an enormous list of URL redirects (5000) to be enforced. The vast majority of the entries on this list all redirect to a generic landing page.

So, the question is is there a way to use the HTTP redirect functionality in the web config to handle specific redirects (oldsite.com/page1 to newsite.com/page1, oldsite.com/page2 to newsite.com/page2, etc etc) and have anything that's not in the list of specific redirects be directed to the generic landing page?

Thanks, C


Solution

  • Yes, you can use the rewrite module in IIS to handle specific redirects and route content outside the specific redirect list to the generic login page.

    The following is an example of how to set up redirect rules in web.config:

        <rewrite>
            <rules>
                <!-- Specific redirects -->
                <rule name="Redirect page1">
                    <match url="^page1$" />
                    <action type="Redirect" url="https://newsite.com/page1" />
                </rule>
                <rule name="Redirect page2">
                    <match url="^page2$" />
                    <action type="Redirect" url="https://newsite.com/page2" />
                </rule>
                <!-- Add more specific redirects as needed -->
    
                <!-- Catch-all redirect -->
                <!-- Any other URL will be redirected to the generic login page -->
                <rule name="Redirect to Generic Landing Page">
                    <match url=".*" />
                    <action type="Redirect" url="https://newsite.com/generic-landing-page" />
                </rule>
            </rules>
        </rewrite>