Vojtěch Struhár

Posted on June 3, 2025 | #astro , #caddy , #self-host

Astro 404 page with Caddy

To create a custom 404 page in Astro, just create a src/pages/404.astro file. During development, Astro will show this page if you go to a non-existent path, which is pretty neat.

I’m using Astro to just build HTML files, which are served by Caddy from my VPS. So let’s instruct it to serve the 404 page if the requested route doesn’t exist.

vojtechstruhar.com, www.vojtechstruhar.com {
	root * /var/www/personal_website
	file_server
	handle_errors {
		@404 {
			expression {http.error.status_code} == 404
		}
		rewrite @404 /404.html
		file_server
	}
}

This is the bespoke configuration this very website is using.

Note

Read this article if you are using Apache and not Caddy!

Read next → How to dualboot PopOS and Windows