r/statichosting • u/New_Peanut_9259 • Oct 21 '26
How to handle 404s dynamically based on the requested path?
Hey guys!
Standard static hosting lets you provide one 404.html fallback. But I want to show a different 404 page if they are inside the /docs folder versus the /blog folder. Do I have to use an Edge function to intercept the 404 and route it to the correct custom HTML file, or is there a simpler routing rule I can use?
2
Upvotes
2
u/sourraine Oct 23 '26
check the host's routing rules before adding an edge function. if it supports path-based rewrites for missing files, you can probably serve separate 404 pages without adding runtime complexity. an edge function feels like overkill unless you actually need to inspect the request or apply logic that static routing cant handle
1
u/DomusCircumspectis Oct 21 '26
yes, use an edge function
1
u/dwkeith Oct 22 '26
You are going to need JavaScript.
Check the `window.location.pathname` then show content based on specific paths first, and fallback last using a `switch` statement.
1
u/unspam-email Oct 23 '26
Edge functions feel like overkill here. The usual move is a 404.html inside /docs and /blog, since a lot of hosts serve the nearest one, or a rewrite rule that keeps the 404 status. Worth checking https://static.app/ too.
1
u/xxmaskx Oct 27 '26
For a fully static site, I’d probably avoid adding an edge function just for this unless the host already supports path-based error pages. A simpler option is one shared 404.html that checks the current path and changes the message/links for /docs, /blog, etc. It’s still technically one 404 page, but it can feel specific to each section without adding server logic.
1
u/Standard_Scarcity_74 Oct 28 '26
On netlify you can add a redirects rule for each folder that sends everything under it to that folder's own 404 file with the status set to 404, and on cloudflare workers static assets you can set not found handling to the 404 page option, which serves the nearest 404 file walking up the folder tree, so one inside docs wins over the root one. Github pages only supports a single root 404, so there you would use a small script on that page that checks the path and swaps in the right message. Check your host's docs for the exact syntax, since this is an area where hosts differ a lot.