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.
Before going through this tutorial, you should already have created a custom 404 page and saved it into your website's directory.
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
Now visiting unavailable pages on your site should display your custom 404 page!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now
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 ;)
@dave: In newer versions of Ubuntu and Debian, the file is called
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:
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?
@bjurkowski: Try editing your virtualhost file (in
/etc/apache2/sites-enabled) and setting
All. Make sure you restart Apache afterwards:
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.
I’m running Apache 2.4.7 courtesy of the DigitalOcean LAMP stack on Ubuntu 14.04