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.
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!
It worked! Thx!
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
@bjurkowski: Try editing your virtualhost file (in
/etc/apache2/sites-enabled
) and settingAllowOverride
toAll
. Make sure you restart Apache afterwards: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?
@dave: In newer versions of Ubuntu and Debian, the file is called
default-000.conf
:You are right! With all due respect I want tell all of you people my experience:
We only need modify a file with a
.conf
extension, searchdefault-000.conf
,000-default.conf
,default.conf
or else in/etc/apache2/sites-enabled/
, add this line (or modify) «AllowOverride None
» to «AllowOverride All
» . If none exists, name as you want (I named itjimmy.conf
) with only this:Just note «Directory /var/www»: this will be applied to all sub directories; just execute
sudo apache2ctl configtest
and you must get aSyntax OK
(otherwise return and check every step); after that runsudo systemctl restart apache2
.Enabling .htaccess has it cost about security, please read for further details, written by Etel Sverdlov too: How To Use the .htaccess File
Got any other problem? just visit: Apache Basics: Installation and Configuration Troubleshooting by Kathleen Juell.
== Additional notes ==
If you have virtual hosts please read for every .htaccess file (ErrorDocument 404) and locations:
*If you want a nice 404 page -and other stuffs-* please visit this libre software project on Github:HttpErrorPages
<Thanks for your attention!>
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 ;)
@mogosselin I had to completed that additional step too. Now the 404 pages is working for me perfectly… not sure why or how, but it’s all golden!
The .htaccess file is working but I am getting standard Apache “Page Not Found” Page, but I want to put my custom page
nice explanation Etel… thanks
Great method told this method is working for my site.
Thank you verryyy much !!