Apache’s default error pages are functional but ugly — and worse, they advertise the server version and module configuration to anyone hitting a 404 or a 500. Replacing them with branded pages takes 10 minutes; making them work correctly for API endpoints (JSON, not HTML), for proxied upstreams, and for 5xx errors during outages takes a bit more care. This article is the full setup.
How to verify
Confirm the current behavior — what does a deliberate error look like on the site right now:
curl -sI https://example.com/this-does-not-exist
curl -s https://example.com/this-does-not-exist | head -20
grep -rE 'ErrorDocument' /etc/apache2/sites-enabled/ /etc/apache2/conf-enabled/
ls /var/www/example.com/public/errors/ 2>/dev/null
The headers should include a Content-Type matching the requested resource type. Hitting /api/does-not-exist should ideally return JSON, not HTML; hitting /blog/does-not-exist should return HTML with the site’s branding. Default Apache returns HTML for both, with the server signature embedded.
What’s happening
The ErrorDocument directive maps a status code to a response. Three forms:
ErrorDocument 404 "Not found"— inline string response. Plain text only.ErrorDocument 404 /errors/404.html— internal redirect to a path inside the same vhost. Apache handles the redirect server-side; the URL in the browser does not change.ErrorDocument 404 https://example.com/errors/404— external redirect (302). Avoid; the browser shows the new URL, and the status code becomes 302 not 404 from the client’s perspective.
The internal-redirect form is the right pattern for branded pages. Apache serves the file as if the client had requested it directly, with the original status code preserved.
For API endpoints, you typically want JSON instead of HTML. The trick is to map the same error code to different responses based on the request — usually via SetHandler and conditional ErrorDocument inside a <Location> block.
The procedure
-
Create the error page templates. Drop them in a path outside the application’s URL structure to avoid collisions:
sudo mkdir -p /var/www/example.com/public/errorsA minimal HTML 404:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Page not found · Example</title> <style>body{font:16px/1.5 system-ui;max-width:640px;margin:80px auto;padding:0 16px;color:#111}h1{font-size:28px}</style> </head> <body> <h1>Page not found</h1> <p>The page you requested does not exist. Try the <a href="/">home page</a> or search.</p> </body> </html>And a minimal HTML 500:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Something went wrong · Example</title> <style>body{font:16px/1.5 system-ui;max-width:640px;margin:80px auto;padding:0 16px;color:#111}h1{font-size:28px}</style> </head> <body> <h1>Something went wrong</h1> <p>We are looking into it. Please try again in a few minutes.</p> </body> </html> -
Wire ErrorDocument in the vhost. Inside the
<VirtualHost>:ErrorDocument 400 /errors/400.html ErrorDocument 401 /errors/401.html ErrorDocument 403 /errors/403.html ErrorDocument 404 /errors/404.html ErrorDocument 500 /errors/500.html ErrorDocument 502 /errors/502.html ErrorDocument 503 /errors/503.html ErrorDocument 504 /errors/504.html -
Make sure the error page directory itself does not 403. Override
<Directory>permissions if needed:<Directory /var/www/example.com/public/errors> Options -Indexes AllowOverride None Require all granted </Directory> -
Configtest and reload.
sudo apache2ctl configtest sudo systemctl reload apache2 -
Verify with curl. Hit a deliberate 404:
curl -sI https://example.com/does-not-exist curl -s https://example.com/does-not-exist | head -5The status line should be
HTTP/2 404, and the body should be your branded HTML. TheServerheader should not reveal the version (assumingServerTokens Prodis set). -
For API endpoints, return JSON instead. Inside a
<Location>block:<Location /api> ErrorDocument 400 /errors/json/400.json ErrorDocument 401 /errors/json/401.json ErrorDocument 403 /errors/json/403.json ErrorDocument 404 /errors/json/404.json ErrorDocument 500 /errors/json/500.json </Location>And the JSON content (
404.json):{"error":"not_found","message":"The requested resource does not exist."}Make sure the JSON files are served with
Content-Type: application/json:<Files "*.json"> Header set Content-Type "application/json" </Files> -
Handle 503 during planned maintenance. Pair the maintenance flag pattern from mod_rewrite basics with a static 503 page:
RewriteCond /var/www/.maintenance -f RewriteCond %{REQUEST_URI} !^/errors/ RewriteCond %{REQUEST_URI} !^/static/ RewriteRule .* /errors/503.html [R=503,L] -
For proxied upstreams, configure
ProxyErrorOverride. By default, an error from the upstream (e.g. a 502 returned by a Node app) is passed through to the client verbatim. To have Apache substitute its own error page:ProxyErrorOverride On ErrorDocument 502 /errors/502.html ErrorDocument 503 /errors/503.htmlWithout
ProxyErrorOverride On, the upstream’s error response wins and your branded page is bypassed.
Common pitfalls
- An
ErrorDocumentpath that does not exist falls back to Apache’s default error page silently — verify each path resolves. ProxyErrorOverride Onoverrides the upstream’s response body too, which means an API that returns structured error JSON gets its body replaced. Scope this to the front of the site, not the API.- A
<Location /api>ErrorDocument only applies to errors generated within that location — a 404 from outside/api(e.g. requesting/apii/foo) falls back to the vhost-level error page. - Internal redirect to
/errors/404.htmlrequires Apache to serve the static file; ifmod_rewriterules route everything through a front controller (/index.php), the front controller intercepts and the static file is never served. Exclude/errors/from the rewrite. - For
ErrorDocumentto set the right status, the page itself must not be a.phpscript that emits its own headers — use static HTML for predictable behavior.
Stack Harbor configures branded error pages with content-type-appropriate responses (HTML, JSON) on every Apache vhost we manage, with a 503 page wired into the maintenance flag pattern. The default Apache error pages reveal too much and serve no business purpose; replacing them is a one-time cleanup in the Managed Operations onboarding.