Rewrite empty URL only

The caddyfile below has been working for a year or more now. Last week I updated Caddy for the first time in about a year (from 0.10.11 to 0.11.2), and now I have a problem. It seems to me that the rewrite line is now rewriting everything instead of just an empty request.

Has something changed, or is there an issue?

The commented out rewrite was my attempt to work round it, but it cause a hang.

Paul, {
        rewrite / /magnoliaPublic/ai/index.html
#	rewrite {
#		pattern /(.*)
#		if {1} is ""
#		to /magnoliaPublic/ai/index.html
#	}
	proxy /magnoliaPublic http://localhost:8080 
	proxy /dms http://localhost:8080/magnoliaPublic/dms/ai/ {
		without /dms
	proxy / http://localhost:8080/magnoliaPublic/ai/

	filter rule {
		content_type text/html.*
		search_pattern /magnoliaPublic/dms/ai/
		replacement /dms/
	filter rule {
		content_type text/html.*
		search_pattern /magnoliaPublic/ai/
		replacement /

	log .\Logs\AIaccess.log
	errors .\Logs\AIerror.log

As far as I can remember, / has always been the default base path, and it’s effectively catch-all.

If you want to rewrite only when the path is exactly /, you can test for that:

rewrite {
  if {path} is /
  to /magnoliaPublic/ai/index.html

I expect instead of pattern in your workaround, you’d want regexp instead; possibly that was the cause of the hang, but you don’t need to pattern match anyway, it’s just an unnecessary expense computationally when you can just compare to {path}.

My original rewrite worked before, and was written like that because it is explicitly documented under Rewrite! The first example says:

Rewrite everything to /index.php. ( `rewrite / /index.php`  will only match /)

`rewrite .* /index.php`

where the stated exception (which I’ve also seen mentioned in forum messages) is what I wanted.

But your workaround does fine, thanks!


1 Like

Ahh, yep, it’s been so long since I’ve used a one-liner rewrite!

That’d be a bug indeed; either the docs or the behaviour is incorrect.

I would classify it as a bug in the code, unless an explicit decision has been made to change behaviour which has formerly been decided upon and documented, and may have been used by others.

At the least, if a change of behaviour is decided on (which I could understand), it should be documented as a change in the release notes (indeed, I looked there before reporting this).


I think it was changed in this PR:

Discussed here:

That was added in version 0.10.13 which was noted as “rewrite: Now supports regular expressions on single-line”.

It wasn’t a 100% backward-compatible change depending on the caddyfile and wasn’t mentioned as such in the release note.

1 Like

Oops. We’ll be more careful about that after a stable 1.0.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.