Creating custom status pages in Caddy v2

Hello,
I am also trying to manage custom error templates. I have read this Caddy 2 handle_error examples with custom error pages? - #8 by francislavoie which helped me for this Caddyfile setup.

Let’s say I want to redirect all “errors” status on a specific url /error with a custom template.
So far, here is what is working :

handle_errors {
    @404 {
        expression {http.error.status_code} == 404
    }
    redir @404 /error
}

route /error {
	rewrite * /lib/tpl/error.php
}

Using the ‘in [404,403,500]’ syntax also works for the expression.

I also can use the handle directive instead of route :

handle /error {
	rewrite * /lib/tpl/error.php
}

I am not sure about the difference but the handle seems prefereable as it is just an internal rewrite.

Here is the json part for this :

I hope it can help even if I feel it’s not perfect.

Thanks for sharing :slight_smile: