Create a custom 404 error page for static sites with Caddy

If you are using Caddy 2 web server, the handle_errors directive allows creating a new rule for serving a custom page on a 404 error.

In this example I have created a custom 404 template in a file named 404.php and the following directive is set: {
    root * /var/www/html/

    handle_errors {
        @404 {
            expression {http.error.status_code} == 404
        rewrite @404 /404.php
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.