'Is it possible to use Application Gateway rewrites rules to redirect to external website?

I have a task to use Application Gateway rewrite rules to capture any request to (*).mycompany1.com and redirect it to (*).mycompany2.com. I created rewrite set which has a single condition of matching request header Host to (.*)mycompany1.com and single action for Response Location header Location to be set to {http_req_Host_1}mycompany2.com My expectation is that once request comes in, response HTTP 302 is generated with Location header mentioned above. It's not working, instead request is being made to backend and Location header just added to a response. How do I force rewrite rule to return HTTP 302?



Solution 1:[1]

• I suggest you to please understand the process of modification of a redirection URL in an application gateway as your requirement is to generate an HTTP 302 response with location as a header added when the request comes in. This can be seen as a need to modify a redirection URL which sometimes comes up in the context of a configuration whereby Application Gateway is configured to override the hostname towards the backend. The hostname as seen by the backend is in that case different from the hostname as seen by the browser. In this situation, the redirect would not use the correct hostname. This configuration is not recommended.

Also, when an app service receives the request to redirect and when it responds, it uses the same hostname in the location header of its response as the one in the request it receives from the application gateway. Thus, the client will make the request directly to the backend website hosted instead of going through the application gateway due to which in your case, the ‘Location’ header is just added to the response.

• Since you want a HTTP 302 response with ‘Location’ header mentioned above, you will have to set the hostname in the location header to the application gateway's domain name. To do this, you will have to create a rewrite rule with a condition that evaluates if the location header in the response contains the application gateway’s URL. For example, enter the pattern ‘(https?)://.azurewebsites.net(.)$.’ in it and create an action to rewrite the location header so that it has the application gateway's hostname. Do this by entering ‘{http_resp_Location_1}://contoso.com{http_resp_Location_2}’ as the header value. Alternatively, you can also use the server variable host to set the hostname to match the original request. Please find the screenshot below for more information: -

Application gateway rewrite rules

Please find the documentation link below for more information: -

https://docs.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers-url#modify-a-redirection-url

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KartikBhiwapurkar-MT