Learn how to create a custom 404 error page using the .htaccess file on Apache. Follow this step by step guide to configure ErrorDocument, improve user experience, and display a professional 404 page for your website.
When someone visits a page that no longer exists on your website, they usually see a standard 404 Not Found message. While this page works, it doesn't help visitors find what they were looking for.
Creating a custom 404 error page gives your website a more professional look and helps users continue browsing instead of leaving your site.
What is a 404 Error Page?
A 404 Error appears when a visitor requests a page that cannot be found on your web server.
Common reasons include:
- A page was deleted.
- The URL was typed incorrectly.
- A link points to a page that no longer exists.
- The page was moved without a redirect.
A custom 404 page allows you to display your own message, add navigation links, search boxes, or branding.
Prerequisites
Before you begin, make sure you have:
- An Apache web server
- Access to your website files
- Permission to edit the
.htaccessfile - A text editor such as Nano or Vim
Step 1: Go to Your Website Directory
Open your terminal and move to your website's document root.
Example:
cd /var/www/html
If your website is located elsewhere, navigate to that directory instead.
Step 2: Create a Custom 404 Page
Create a new HTML file named 404.html.
nano 404.html
Add a simple custom page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Not Found</title>
</head>
<body>
<h1>404</h1>
<p>Sorry, the page you are looking for could not be found.</p>
<p><a href="/">Return to Home</a></p>
</body>
</html>
Save the file.
If you are using PHP, you can also create:
404.php
Step 3: Edit the .htaccess File
Open the .htaccess file in your website directory.
nano .htaccess
If the file does not exist, simply create it.
Step 4: Add the Custom 404 Rule
Add the following line.
ErrorDocument 404 /404.html
If you created a PHP page instead, use:
ErrorDocument 404 /404.php
The path begins with a forward slash because it is relative to your website's document root.
Save the file after making the changes.
Step 5: Check Apache Configuration
Before testing, verify that the Apache configuration is valid.
sudo apachectl configtest
If everything is correct, you should see:
Syntax OK
Step 6: Reload Apache
Apply the changes by reloading Apache.
Ubuntu and Debian
sudo systemctl reload apache2
RHEL, Rocky Linux, AlmaLinux, and CentOS
sudo systemctl reload httpd
Step 7: Test the Custom 404 Page
Open your browser and visit a URL that does not exist.
Example:
https://yourdomain.com/this-page-does-not-exist
Instead of the default Apache error page, your custom page should appear.
Example .htaccess File
A basic .htaccess file with a custom 404 page looks like this.
ErrorDocument 404 /404.html
You can also define other custom error pages.
ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
Tips for a Better 404 Page
A good custom 404 page should help visitors continue browsing your website.
Consider including:
- Your website logo
- A clear message
- A Home button
- Navigation menu
- Search box
- Links to popular pages
- Contact page link
Keeping the page simple and easy to navigate provides a better user experience.
Troubleshooting
Custom page is not displayed
Make sure the .htaccess file is inside your website's document root and the ErrorDocument path is correct.
Apache ignores the .htaccess file
Verify that Apache allows .htaccess overrides.
Your Virtual Host configuration should include:
<Directory /var/www/html>
AllowOverride All
</Directory>
Reload Apache after making any configuration changes.
404 page returns another 404 error
Confirm that the 404.html or 404.php file exists in the correct location and has the proper file permissions.
Conclusion
A custom 404 error page is a simple improvement that makes your website look more professional and helps visitors stay engaged. By creating a custom page and adding a single ErrorDocument directive to your .htaccess file, you can replace the default Apache error page with your own design. You can also create custom pages for other HTTP error codes using the same approach, giving your website a consistent and polished experience.

