How to Configure a Custom 404 Page in Apache?

Configuring a custom 404 “Not Found” error page in Apache enhances user experience and maintains brand consistency when visitors hit broken links. This article provides a straightforward guide on how to set up a custom error page using the .htaccess file or the main Apache configuration file, along with testing steps to ensure it works correctly.

Step 1: Create Your Custom 404 HTML Page

Before changing any server settings, you need to design and upload the actual page users will see.

Step 2: Modify the Configuration File

You can point Apache to your custom error page using either a local .htaccess file or the global server configuration.

If you are on shared hosting or want to configure this per website without restarting the server, use the .htaccess file located in your website’s root directory.

  1. Open your .htaccess file in a text editor. If it doesn’t exist, create a new file named exactly .htaccess.
  2. Add the following line of code:

ErrorDocument 404 /404.html

Note: The forward slash / before 404.html indicates the root directory of your website. It is best to use a relative path like this rather than an absolute URL (e.g., https://example.com/404.html), as an absolute URL can cause Apache to send a “200 OK” status instead of a true “404 Not Found” status to search engines.

Method B: Using the Apache Virtual Host Configuration

If you have VPS or dedicated server access, you can apply this globally or per virtual host in your main configuration files (such as httpd.conf or apache2.conf).

  1. Open your virtual host configuration file.
  2. Inside the <VirtualHost *:80> or <VirtualHost *:443> block, add the same directive:

ErrorDocument 404 /404.html

  1. Save the file and restart Apache to apply the changes:

sudo systemctl restart apache2 (for Ubuntu/Debian) sudo systemctl restart httpd (for CentOS/RHEL)

Step 3: Test the Configuration

To verify that your custom error page is functioning correctly: