Wildcard certificate and handle_errors with by-host error pages

1. The problem I’m having:

Hi, when using wildcard certificate, how to configure handle_errors to respond with specific host’s error page?

Here is an example Caddyfile that I would expect working, but it is not.
The purpose is to respond with custom 404 error page, but each host has its own one.

*.domain.com {
	@subA {
		host suba.domain.com
	}
	handle @subA {
		file_server
		route {
			root * /var/www/suba
		}
	}

	@subB {
		host subb.domain.com
	}
	handle @subB {
		file_server
		route {
			root * /var/www/subb
		}
	}

	handle_errors {
		@subA_err {
			host suba.domain.com
			expression `{http.error.status_code} == 404`
		}
		handle @subA_err {
			rewrite * /var/www/suba/404.html
			file_server
		}

		@subB_err {
			host subb.domain.com
			expression `{http.error.status_code} == 404`
		}
		handle @subB_err {
			rewrite * /var/www/subb/404.html
			file_server
		}
	}
}

Perhaps in the provided placeholders, adding one for the matched domain could help? This would allow for such expression

@subA `{err.status_code} == 404 && {err.host}.contains("subA")`

3. Caddy version:

v2.10.2

4. How I installed and ran Caddy:

docker

Finally, I figured out I had a cache issue, the example Caddyfile I shared actually works.

2 Likes