Tutorial

How To Create a Custom 404 Page in Apache

Published on July 10, 2012
How To Create a Custom 404 Page in Apache

Why Create a Custom 404 Page

A custom 404 page lets you provide a user-friendly website to your visitors even in the midst of an error. Very few users, when presented with a 404, will do more beyond click back to get out of the mistake. A custom 404 page is a good opportunity to keep them on your site and do more to redirect them to their destination.

Setup

Before going through this tutorial, you should already have created a custom 404 page and saved it into your website's directory.

Implement the 404 Page

To edit the 404 page, open up or create the site's .htaccess file. You can create it in a text editor and upload it to your site via the FTP server. Keep in mind that the name of the file has to be simply .htaccess.

Add the following line to the file, replacing new404.html with the correct new error page name:

ErrorDocument 404 /new404.html

Save and Exit.

Keep in mind that the Apache looks for the 404 page located within the site's server root. Meaning that if you place the new error page in a deeper subdirectory, you need to include that in the line, making into something like this:

ErrorDocument 404 /error_pages/new404.html

See the 404 Page

Now visiting unavailable pages on your site should display your custom 404 page!

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I installed the default LAMP config and I added this at the end of the file (outside of the </virtualhost> tag) <Directory /var/www/html/> Options Indexes FollowSymLinks AllowOverride All </Directory>

And it worked. Just adding AllowOverride All anywhere won’t work ;)

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 13, 2014

@dave: In newer versions of Ubuntu and Debian, the file is called default-000.conf:

sudo nano /etc/apache2/sites-enabled/default-000.conf

A quick caveat: be careful when using dot files (files and directories which begin with a period) in configuration settings. Because of past security issues, such as attackers inserting “/./” into a URL to bypass access restrictions, dot files are handled differently by default. For example, I tried following the advice in this tutorial, using this config line:

ErrorDocument 404 /.l/new404.html

It worked fine when the config line and failed URL were both inside the “.l” directory, but then failed in every other case. Once I changed the directory “.l” to a name that did not begin with a period, all was well.

Using other configuration settings such as “FilesMatch”, it’s surely possible to convince Apache to handle the dot files as intended, but I didn’t bother. The special handling helps stop subtle errors, and testing for every relevant attack and edge case would be far more work than simply avoiding dot-file names in the ErrorDocument path.

Thank you verryyy much !!

Great method told this method is working for my site.

nice explanation Etel… thanks

The .htaccess file is working but I am getting standard Apache “Page Not Found” Page, but I want to put my custom page

When I "sudo nano /etc/apache2/sites-enabled/default-000 it is blank and I am unable to edit anything. Not sure where my virtualhost file is. Any suggestions?

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 4, 2014

@bjurkowski: Try editing your virtualhost file (in /etc/apache2/sites-enabled) and setting AllowOverride to All. Make sure you restart Apache afterwards:

sudo service apache2 restart

Where might I start troubleshooting the fact that this doesn’t work on my server? I created a 404.php then created a .htaccess file in /var/www/html/ which contains the following: ErrorDocument 404 /404.php

Yet, when navigating to any URL that does not exist on my website, I get the usual Apache 404 text.

Any thoughts?

I’m running Apache 2.4.7 courtesy of the DigitalOcean LAMP stack on Ubuntu 14.04

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel