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.
- Create a file named
404.html(or any name you prefer). - Add your custom HTML, CSS, and branding to the file.
- Upload this file to the root directory of your website (usually
public_htmlorvar/www/html).
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.
Method A: Using the .htaccess File (Recommended)
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.
- Open your
.htaccessfile in a text editor. If it doesn’t exist, create a new file named exactly.htaccess. - Add the following line of code:
ErrorDocument 404 /404.html
Note: The forward slash
/before404.htmlindicates 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).
- Open your virtual host configuration file.
- Inside the
<VirtualHost *:80>or<VirtualHost *:443>block, add the same directive:
ErrorDocument 404 /404.html
- 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:
- Open your web browser.
- Navigate to your website and type a deliberate typo at the end of
the URL (e.g.,
https://yourdomain.com/thispage-does-not-exist). - Verify that your custom
404.htmlpage displays instead of the default, generic Apache error message.