How to rewrite application gateway from abc.com to abc.com/abc?

Loknath Bhowmik 0 Reputation points
2023-02-13T10:52:36.65+00:00

when user hit the url www.abc.com it should redirect to www.abc.com/abc

abc.com is configured on listener

Azure Application Gateway
Azure Application Gateway
An Azure service that provides a platform-managed, scalable, and highly available application delivery controller as a service.
1,213 questions
{count} votes

2 answers

Sort by: Most helpful
  1. MUHAMMAD IMRAN 15 Reputation points
    2023-02-13T11:18:40.1133333+00:00

    To redirect requests from "abc.com" to "abc.com/abc", you can use the URL Rewrite feature in Application Gateway. Here's how:

    1. Create a new URL Rewrite rule: In the Azure Portal, go to your Application Gateway resource and click on the "URL Rewrite" blade. Then, click on "Add URL Rewrite Rule."
    2. Configure the rule: In the rule configuration, specify the following settings:
      • Name: Give the rule a descriptive name, such as "Redirect to /abc".
      • Requested URL: Choose "Matches the Pattern."
      • Pattern: Enter "^/$". This will match the root URL (abc.com).
      • Action type: Choose "Redirect."
      • Redirect URL: Enter "/abc".
      • Redirect type: Choose "Permanent (301)."
    3. Save and apply the rule: Save the URL Rewrite rule, and then apply the changes to your Application Gateway.

    With this URL Rewrite rule in place, any request to "abc.com" will be redirected to "abc.com/abc". Keep in mind that this will only work for requests that match the specified pattern. If you need to redirect additional URLs, you can create additional URL Rewrite rules as needed.


  2. GitaraniSharma-MSFT 50,021 Reputation points Microsoft Employee Moderator
    2023-03-03T11:42:09.9133333+00:00

    Hello @Loknath Bhowmik ,

    Apologies for the delay in response.

    I understand that you would like to know how to rewrite Application gateway URL from abc.com to abc.com/abc.

    You've mentioned the question heading as "rewrite application gateway from abc.com to abc.com/abc" but the question says "when user hit the URL www.abc.com it should redirect to www.abc.com/abc"

    I'm not sure if your requirement is a rewrite or a redirect.

    Below is the difference between URL rewrite and URL redirect in Application gateway:

    For a URL rewrite, Application Gateway rewrites the URL before the request is sent to the backend. This won't change what users see in the browser because the changes are hidden from the user.

    For a URL redirect, Application Gateway sends a redirect response to the client with the new URL. That, in turn, requires the client to resend its request to the new URL provided in the redirect. The URL that the user sees in the browser will update to the new URL.

    Refer: https://learn.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers-url#url-rewrite-vs-url-redirect

    If you need to rewrite the URL (the URL won't change on the browser, but the rewritten URL will be sent to backend), then you can use URL rewrite feature of Application gateway as below:

    User's image

    Refer: https://learn.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers-url

    https://learn.microsoft.com/en-us/azure/application-gateway/rewrite-url-portal

    If you need to redirect the URL (the URL changes in the browser to abc.com/abc when a user hit abc.com), then this isn’t possible via redirect rules out of box but can be possible via a workaround.

    You can refer the below article where I've provided the steps for such a configuration:

    https://learn.microsoft.com/en-us/answers/questions/1088773/appgw-homepage-only-redirect-but-other-forward-to

    NOTE: When setting the above configuration, you need to make sure that abc.com/abc is resolvable.

    Below is the same setup replicated in my lab:

    (Note: I didn't have a custom domain, so I used a DNS name for the Application gateway to replicate the same)

    Multi-site Listener with host name (in you case the hostname would be abc.com and your Application gateway IP pointed to abc.com)

    User's image

    Rules configured for normal backendpool with 2 more additional rules - one for redirect and one for path of /abc to work:

    User's image

    The redirect rule should be for the placeholder path that is added in the rewrite section as below:

    User's image

    I used an app service in the backend with a virtual directory with path /abc in it and since I didn't have a custom domain, the URL redirects to app service URL as per this known issue but it works:

    When I try to hit the application gateway URL i.e. http://abc-appgw.eastus.cloudapp.azure.com/, it redirects to http://myappgwapp1.azurewebsites.net/abc/

    User's image

    Kindly let us know if the above helps or you need further assistance on this issue.


    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.