Skip to main content
Changed wording (less intense) and included reference link
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

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:

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 tag - unadvisable) 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.

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 tag - but 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:

added 257 characters in body
Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

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 tag - unadvisable) 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.

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 tag - unadvisable) 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".

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 tag - unadvisable) 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.

Source Link
MrWhite
  • 43.1k
  • 4
  • 50
  • 90

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 tag - unadvisable) 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".