This is because the Caddyfile sorts directives based on a pre-defined order:
You’ll notice that redir is higher than reverse_proxy.
As noted in the docs, you can use the route directive to override the order, or you could use a not matcher to invert the matching logic. First with the route approach:
Note that the above will only work as-is with the upcoming v2.2.0 release (or latest on the master branch), because there was a bug preventing @ matchers from being defined inside of route blocks. If you prefer this approach, for now, you’ll need to move the @query-matcher line just before the route, but I wanted to show the “ideal” config here.
Alternatively with the negated matcher:
:443 {
import server
@query-matcher query k=v
reverse_proxy @query-matcher https://foo.local
reverse_proxy /api/* https://foo.local
@redirect {
not path /api/*
not query k=v
}
redir @redirect https://bar.local{uri}
}
Note that I also inlined your /api/* path matcher because it saves a line