You shouldn't have to do anything. You would normally serve the same ErrorDocument
regardless of what URL caused the error. In your case, you are serving /404.html
in the document root of your site.
However, it sounds as if you are using relative URLs to your static resources inside your error document. This is never going to work (unless you set a base
tagset a base
tag - unadvisablebut this is not without its caveats - see reference link below) since the error document could be served from anywhere and so the browser will resolve your relative URLs incorrectly (or at least, not as you intended). Instead, you need to use root-relative URLs (starting with a slash, as you have done for /404.html
) or absolute URLs (including scheme + hostname).
when I access
example.com/parent/nonexistentpage.html
, although the URL doesn't change, I get a generic, unstyled 404 error message
Assuming your ErrorDocument
directive is still set to serve /404.html
and you are using relative paths in /404.html
, then any relative URL-paths will naturally resolve relative to the /parent
"directory", not the document root, as it would otherwise do if requesting example.com/nonexistentpage.html
.
Remember it is the browser that resolves relative URLs, based on the URL being requested. This has nothing to do with the filesystem path on your server.
See this other related question for additional examples and discussion about the use of the base
tag: