Azure APIM Redirect root to a backend based on header setting

SvenGlöckner 436 Reputation points

Hi, is it possible to redirect root traffic to an API backend ? Example: We have an API that's available at /test1. When a call to our root APIM domain is made on it should forward/redirect to /test1 backend based on a header condition, i.e. Header "Backend" with value "test1". I already tried with the error handler policy <on-error> but had no success. I'm using the base policy that's under "All APIs"


            <when condition="@(context.LastError.Reason == "OperationNotFound" && context.Request.Url.QueryString == "")">
                <set-backend-service backend-id="test-1" />
                    <set-status code="303" />
                    <set-header name="Location" exists-action="override">
            <otherwise />

this results in an endless redirect loop.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,809 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 22,236 Reputation points

    SvenGlöckner Thanks for sharing the additional context. You can define an operation with URL as /test1 with needed policy, backend API, configurations etc. Then add a new operation with URL as / and use send-request policy to call /test1 and then return the response using return-response policy. This way default URL will call operation / and executes the policy defined within the operation.

    actual operation (defines /test1) User's image

    sample operation (defines /) User's image

    Policy Snippet:

    <send-request mode="new" response-variable-name="redirectResponse" timeout="60" ignore-error="true">
                <set-header name="Host" exists-action="override">
                <set-header name="Ocp-Apim-Subscription-Key" exists-action="override">
                    <value>@(context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key", ""))</value>
            <return-response response-variable-name="redirectResponse" />

    Note, the above policy is just for reference based on and you can customize it based on your need such as checking additional headers etc.

    I hope this helps with your questions and let me know if you have any other.

    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.