Share via

APIM redirect to another API based on Host

Rahul Prabhu 46 Reputation points
2022-08-18T10:48:11.67+00:00

I have a private APIM (hostname: apim.custom.com) with 2 APIs, dev1 and dev2 with similar set of operations(api/v1)

How can I redirect request from dev1.apim.custom.com/api/v1 to dev1/api/v1 api operation and dev2.apim.custom.com/api/v1 to dev2/api/v1?

*.apim.custom.com is mapped to private IP of APIM in the Private DNS Zone

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.


Answer accepted by question author

MuthuKumaranMurugaachari-MSFT 22,451 Reputation points Moderator
2022-08-19T15:30:18.887+00:00

@Rahul Prabhu Thank you for your reply. Yes, you can create a new API with default API URL suffix (empty) or api/v1 simply for the purpose of redirection. In that API, you can define an operation with URL as "/" and set inbound policy with return-response like below:

    <inbound>  
                <choose>  
                    <when condition="@(context.Request.OriginalUrl.Host == "dev1.apim.custom.com")">  
                        <return-response>  
                            <set-status code="303" />  
                            <set-header name="Location" exists-action="override">  
                                <value>@("https://" + context.Request.OriginalUrl.Host + "/dev1/api/v1")</value>  
                            </set-header>  
                        </return-response>  
                    </when>  
                    <when condition="@(context.Request.OriginalUrl.Host == "dev2.apim.custom.com")">  
                        <return-response>  
                            <set-status code="303" />  
                            <set-header name="Location" exists-action="override">  
                                <value>@("https://" + context.Request.OriginalUrl.Host + "/dev2/api/v1")</value>  
                            </set-header>  
                        </return-response>  
                    </when>  
                </choose>  
                <base />  
            </inbound>  

Please make sure to set redirect policy logic above base element.

This way whenever you hit dev1.apim.custom.com/api/v1, based on the condition it should redirect to dev1/api/v1 and similarly for dev2.apim.custom.com/api/v1, it goes to dev2/api/v1. Please validate and let me know if you ran into any issues.

Just a note, make sure that API(s) is grouped accordingly if you enable a subscription key for the product. Also, if needed, restrict other domains from accessing API directly (dev1.apim.custom.com/dev2/api/v1 or dev2.apim.custom.com/dev1/api/v1) as mentioned here.

I hope this answers your question and feel free to add if you have any questions. Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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