Custom Error Pages with .htaccess

Updated November 2026 for Apache 2.4. The complete, current reference is Custom 404 Page and Other ErrorDocuments in THE Ultimate Htaccess.

Custom error pages enhance user experience by providing informative and branded messages instead of default server errors. Using .htaccess files in Apache, you can easily set up custom error pages for various HTTP error codes.

Basic Setup

To create custom error pages, you need to create HTML files for each error you want to handle (e.g., 404.html for Not Found, 500.html for Server Error). Place these files in your web directory.

Next, add the following lines to your .htaccess file to define custom error documents:

ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
ErrorDocument 403 /errors/403.html
ErrorDocument 401 /errors/401.html

Enhancing User Experience

Custom error pages can include helpful navigation links, a search box, and a friendly message to guide users back to the main content of your website.

Example 404.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page Not Found</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
        h1 { font-size: 50px; }
        p { font-size: 20px; }
    </style>
</head>
<body>
    <h1>404</h1>
    <p>Sorry, the page you are looking for does not exist.</p>
    <a href="/">Go to Homepage</a>
</body>
</html>

Advanced Techniques

Error Pages on Another Domain (usually a mistake): You can point ErrorDocument at a full URL:

ErrorDocument 404 https://example.com/errors/404.html

But a full URL makes Apache send a 302 redirect instead of the error status. Search engines then see a redirect, not a 404, and for a 401 the browser never shows the password prompt. Keep error documents on your own site as local paths.

Custom Error Page for Specific Directories: <Directory> blocks only work in the main server config and throw a 500 in .htaccess. Put a separate .htaccess in the directory instead:

# /special/.htaccess
ErrorDocument 404 /special/404.html

Maintenance Mode With a Real 503: A 503 with Retry-After tells crawlers to come back later instead of indexing your "be right back" page. Your own IP gets through.

ErrorDocument 503 /maintenance.html
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=203.0.113.10
RewriteCond %{REQUEST_URI} !=/maintenance.html
RewriteRule ^ - [R=503,L]
Header always set Retry-After "3600" "expr=%{REQUEST_STATUS} == 503"

Reference

For more detailed information and advanced configurations, you can visit AskApache’s guide on custom error pages which provides comprehensive examples and insights.

Conclusion

Implementing custom error pages using .htaccess files not only improves the user experience but also helps maintain a professional and cohesive brand image. By following the steps outlined above, you can easily set up and customize error pages for your website.

Comments