Why Isn't My .Htaccess ErrorDocument 404 Showing Up?

Published August 28, 2024

Problem: .htaccess 404 Error Page Not Displaying

When you set up a custom 404 error page using the ErrorDocument directive in an .htaccess file, the page may not show up as expected. This can be a problem, as the custom error page is meant to improve user experience when visitors find broken links or missing content on a website.

Troubleshooting .htaccess ErrorDocument 404 Problems

Checking .htaccess file configuration

To set up a custom 404 error page using the .htaccess file, use the correct syntax for the ErrorDocument directive:

ErrorDocument 404 /path/to/your/404.php

Make sure the file path is correct. Use a path relative to the document root, not the full server path. For example:

ErrorDocument 404 /404.php

This assumes your 404.php file is in the document root directory.

Tip: Use absolute URLs for external error pages

If your custom error page is on a different domain or subdomain, use an absolute URL:

ErrorDocument 404 http://example.com/error-pages/404.html

This helps avoid potential issues with relative paths when the error occurs in subdirectories.

Verifying Apache settings

The AllowOverride directive in Apache's configuration is important for .htaccess files to work. This directive allows or disables the use of .htaccess files in specific directories.

To check and modify Apache configuration:

  1. Open the main Apache configuration file (often at /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf).
  2. Find the section that applies to your website's directory.
  3. Make sure the AllowOverride directive is set to All or at least FileInfo.

Example:

<Directory /var/www/html>
    AllowOverride All
</Directory>

After making changes, restart Apache to apply the new settings.

Testing .htaccess file functionality

To confirm if Apache is reading your .htaccess file:

  1. Add an invalid line to your .htaccess file, such as:
    ThisIsAnInvalidLine
  2. Try to access any page in the directory with the .htaccess file.
  3. If you see a 500 Internal Server Error, Apache is reading the .htaccess file.
  4. If the page loads normally, Apache is not reading the .htaccess file, and you need to check your AllowOverride settings.