Confusion: rewrite directive help

Hi,

So basically I have read the whole statement so many times, and from it I can conclude so many different things. It is not exactly clear how certain things should be expressed.

The doc states

rewrite (Caddyfile directive) — Caddy Documentation

I will ask the following in order to understand everything in deep; please correct me if my assumptions are incorrect.

Given this example:
When the adapter reads the rewrite directive the next handler especially the header directive in the given example is not synced with the URI path manipulation yet, because it has not been happened yet.Handlers beneath that are deferred until after the rewrite is performed and path is manipulated. So that is the reason why the beauty matcher of respond matches first and header not So far I understand it.

But statement:

In other words, a matcher that matches a request before the rewrite might not match the same request after the rewrite

Is that happening because of the ordering system? And with a route or handle directive we can prevent that and make it all happen according to how all is expressed without re-ordering of directives or deferring?

	rewrite /da /beauty

	header /beauty {
		+Custom-Header "ok"
	}
	respond /beauty 200 {
		body "hello"
	}

Yes.

The header directive has a higher directive order than rewrite, so it gets sorted to be first. So it runs like this, essentially:

	header /beauty {
		+Custom-Header "ok"
	}

	rewrite /da /beauty

	respond /beauty 200 {
		body "hello"
	}

So if your request was /da, then header does not match (and therefore is not run), then the rewrite matches and changes the URL to /beauty, then respond will match and run.

With route, yes, directives within are not sorted, and kept in the literal order in the config.

The handle directive doesn’t change the order of things within it (directives within handle are still sorted) but it does allow you move some directive up or down in order by the fact that handle itself has its own directive order. So if you put header inside of handle, since handle is lower in the order than rewrite, it would cause the header to match (since the rewrite would have happened before handle.

To see what effect the sorting has, the easiest way is to adapt your config to JSON with the caddy adapt -p command, then you can see the order in which handlers are placed in the final config.

Oke thanks, was a bit confused yesterday had to know in deep if my assumption were correct.