How do I route all content with a specific prefix to another service

Not applicable

I want to configure my proxy in the following fashion.

I have tried using RouteRule:

    <RouteRule name="mocker">
        <Condition>(proxy.pathsuffix JavaRegex "^/admin/?.*$")</Condition>
        <TargetEndpoint>MockTarget</TargetEndpoint>
    </RouteRule>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>

This partially works in that is does indeed route to a different host. However, it maintains the path; so requesting /admin/foo/bar gets redirected to http://admin.api.com/admin/foo/bar, but I wanted it to go to http://admin.api.com/foo/bar

How can I achieve that?

1 3 241
3 REPLIES 3

If you want to eliminate the proxy-path from the target-path, do not forget to set the context variable `target.copy.pathsuffix` to false. This also must be done in the target flow. You can either use a JavaScript or AssignMessage policy to enforce it.

See this post for sample implementations.

I think you misunderstand. I don't want it to ignore the suffix completely. I just want to only append the part after '/admin'. So it is not a binary choice, I just want modify the path slightly.

ah I see... AFAIK you can't set the URL partially. But you can to use a JavaScript like this -

context.setVariable("target.copy.pathsuffix", false);
var targetserver = context.getVariable("target.url");
//instead of hardcoding the value, use Regex of some sort 
var targetpathsuffix = "/foo/bar";
var targeturl = targetserver + targetpathsuffix
context.setVariable("target.url", targeturl);