Astro 404 page with Apache
I recently had the privilege to host a static Astro website on a cPanel server. And since cPanel uses Apache by default, the custom 404 page can be configured by a single line of code.
- Create your 404 site -
pages/404.astro
. If you name it precisely this, Astro will route to it in dev mode! - Create a
public/.htaccess
file. Apache will pick up on this config file automatically, it will just get copied to the server
Put this line into .htaccess
# Redirect all 404 errors
ErrorDocument 404 /404.html
Done, awesome. You could also redirect to your root if that makes sense for you.
Other approaches
- Checking the validity of the route with a
index.php
file (PHP takes precendence over HTML files) - Running Astro in SSR mode (that means to have a Node web server running) and reverse proxying Apache to it
I think compared to those, the .htaccess
file way is a pure beauty.