Azure APIM Redirect URL with pattern

Mohamed Akl 1 Reputation point
2022-04-18T01:04:42.393+00:00

Hello, I have an API management instance (https://my-apim-example.azure-api.net) with one API /product. I need to implement a redirect where APIM redirects to the login page (eg https://www.mywebsite.com/login ) if the request URL does not match my API URL suffix or anything other than /product.

Example: anything other than https://my-apim-example.azure-api.net/product to redirect to https://www.mywebsite.com/login.

How would I do this?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,446 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. VenkateshDodda-MSFT 24,951 Reputation points Microsoft Employee Moderator
    2022-04-18T12:07:25.57+00:00

    @Mohamed Akl Thanks for reaching out. Unfortunately, there is no out of the box solution for your requirement. Please confirm if my understanding is correct that as of now you only have one product in your APIM instance with name product and you want to redirect to login page if the request comes with any other name expects product in the URL as you don’t want to show 404 error to your end user when they hit the invalid URL.

    The workaround would be to create the custom policy at global level as shown in below using [When condition] (https://learn.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#choose) block. The below custom policy will validate whether the API URL contains any suffix with product in the request.

    If the request doesn’t contain suffix product it will redirect to your login page. If the request contains product it will redirect the request to the backend instance.

      <inbound>  
            <choose>  
                <when condition="@(context.Request.Url.Path.Contains("product") ? false : true)">  
                    <return-response>  
                        <set-status code="303" reason="Redirecting" />  
                        <set-header name="Location" exists-action="override">  
                            <value>@("https://https://www.mywebsite.com/login./")</value>  
                        </set-header>  
                    </return-response>  
                </when>  
                <otherwise />  
            </choose>  
        </inbound>  
        <backend />  
        <outbound />  
        <on-error />  
    </policies>  
    

    Note: The above policy is only for reference. If your API instance has more than one product, then you need to modify the above policy by adding multiple when Conditions blocks as per your business use case.

    Feel free to get back to me if you have any query or concern.

    0 comments No comments

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.