Search code examples
azurehttp-redirectazure-web-app-service

Azure App Service redirect with subdomain and parameters


i'm in the process of shutting down an existing old web application and have about 15 URL's which need to be re-directed to our new application;

subdomain.example.com/route?someID=123&otherId=4356

i need to redirect that specific URL to

newsubdomain.newexample.com/route?newID=34563456456

there are about 15 of these URL redirects i need to include. they are specific and perminant.

i have read there is an approach to do this in web.config can this approach be used for multiple URL's ??

cheers


Solution

  • I have read there is an approach to do this in web.config can this approach be used for multiple URL's ??

    Yes, it's possible to redirect multiple URLs using web.config.

    Thanks to @Thomas Ardal for the clear explanation. Refer to this blog for a better understanding of web.config redirects with multiple URLs using rewrite rules.

    Here's the sample Web.config file:

    You can add multiple <rule> elements within the <rules> section to match different URLs and redirect them accordingly.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    
                    <rule name="RedirectRoute1" stopProcessing="true">
                        <match url="route" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" />
                            <add input="{QUERY_STRING}" pattern="someID=123&otherId=4356" />
                        </conditions>
                        <action type="Redirect" url="https://newsubdomain.newexample.com/route?newID=34563456456" redirectType="Permanent" />
                    </rule>
                   
                    <rule name="RedirectRoute2" stopProcessing="true">
                        <match url="old-page" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" />
                        </conditions>
                        <action type="Redirect" url="https://newsubdomain.newexample.com/new-page" redirectType="Permanent" />
                    </rule>
    
                    <rule name="RedirectRoute3" stopProcessing="true">
                        <match url="another-route" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" />
                        </conditions>
                        <action type="Redirect" url="https://newsubdomain.newexample.com/different-route" redirectType="Permanent" />
                    </rule>
    
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    Now after deploying your app to Azure App service, as you are redirecting from a subdomain, you must configure it in Azure.

    Go to Azure - >Select your App Service -> Custom domains -> Add custom domain -> Enter your subdomain.example.com -> once verified bind it to App service.

    enter image description here

    Refer this doc1 and doc2 to know about mapping subdomain to Azure website.

    SO link for reference.

    Also, you can use Rewrite Map as an alternative approach

    Sample Web.config:

    <rewriteMaps>
        <rewriteMap name="RedirectsMap">
            <add key="subdomain.example.com/route?someID=123&otherId=4356" value="https://newsubdomain.newexample.com/route?newID=34563456456" />
            <add key="subdomain.example.com/old-page" value="https://newsubdomain.newexample.com/new-page" />
            <add key="subdomain.example.com/another-route" value="https://newsubdomain.newexample.com/different-route" />
        </rewriteMap>
    </rewriteMaps>
    

    By the above methods you can redirect multiple URLs using web.config.