<p>we are using AIPM with AKS.I am trying to dynamically redirect the target URL based on the availability. For e.g say I have couple of backend url configured with load balancer. and when I hit the load balancer I want to to go to endpoint 1 or endpoint 2 based on the availability</p>
<p>I am tryin to do something similar as mentioned here in the HA proxy <a href="https://www.haproxy.com/blog/failover-and-worst-case-management-with-haproxy/" rel="nofollow noreferrer">https://www.haproxy.com/blog/failover-and-worst-case-management-with-haproxy/</a>. Since we already using APIM I am assuming it would be a overkill to have HA proxy just for this unless the feature is not available. while I can understand we can rewrite the URL or set the backend dynamically I am not sure if APIM can decide whether the URL is up and if not it can fallback to other URL's mentioned in the config something available in the link above.</p>
<p>What my current understand is that I can use the policy and if there is an error I can use an error policy where I can redirect. but that is for one condition. Any suggestion or pointers would be helpful.</p>

threeleggedrabbit
<p>You can react on the response code from your backend API and set a new backend url. This might not be perfect but it will get you started. In this example I assume that you get a <code>500</code> response from your backend if it's &quot;down&quot;:</p>
<pre><code>&lt;backend&gt;
    &lt;retry condition=&quot;@(context.Response.StatusCode == 500)&quot; count=&quot;2&quot; interval=&quot;1&quot; first-fast-retry=&quot;true&quot;&gt;
        &lt;choose&gt;
            &lt;when condition=&quot;@(context.Response != null &amp;&amp; context.Response.StatusCode == 500)&quot;&gt;
                &lt;set-backend-service base-url=&quot;https://my-other-service.azurewebsites.net&quot; /&gt;
            &lt;/when&gt;
            &lt;otherwise /&gt;
        &lt;/choose&gt;
        &lt;forward-request /&gt;
    &lt;/retry&gt;
&lt;/backend&gt;
</code></pre>
