I hesitate to suggest this because of its implications on complexity, but it’s on my mind so I want to jot it down before I forget and at least entertain some discussion.
The Caddyfile uses a declarative syntax (ordering of the directives mostly doesn’t matter). Sometimes this is problematic, like when people want redir to happen before rewrite.
Now… again, I hesitate to suggest this, because it borderlines turns the Caddyfile into some sort of scripting language which I really want to avoid because complexity. But would it be useful in general (not just the redir/rewrite case) to be able to define a block where directives are executed in the order they appear? For example:
On principle, I’m FOR making more tools available to the user.
That said, I don’t see a use case for it right now. The only ordering question that enters my mind is redir vs rewrite, and I’d rather see them combined into one directive than have to specify their ordering. At the moment, I find the static order works well.
The particular problem with the order of redir and rewrite could be solved with the proposal of making the redir behavior a property of rewrite (see https://github.com/mholt/caddy/pull/1750). That with the fact that all rewrites will be processed in the order they appear should give all power needed for this.
A much more useful change to Caddyfile’s syntax would be to create “locations”, kind of similar with nginx. Instead of every single directive having to process path restrictions in it own way (lots and lots of duplication and poor implementations), have a primitive match location, including the conditionals that rewrite has. This also keeps the syntax fully declarative.