Create a custom 404 error page for static sites with Caddy

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:

pcx3.com {
    root * /var/www/html/pcx3.com/public
    file_server

    handle_errors {
        @404 {
            expression {http.error.status_code} == 404
        }
        rewrite @404 /404.php
        file_server
    }
}
whoami
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.