How To Create a Custom 404 Page in Apache


how to create custom 404 page

When you run a website, it is likely that sometimes, some of your pages are unavailable to your website visitors. It may be because you deleted them, or moved them to a different location. However, when users visit a page that does not exit, they are shown a terse “404: page not found” message by default. In such cases, they will most likely leave your site. Instead, if you show them a custom 404 page with next steps listed clearly, it will keep them from leaving your site and direct them to the right place. So let us look at how to create a custom 404 page in Apache.

 

How To Create a Custom 404 Page in Apache

 

Create a 404 Page

Create a custom 404 page in HTML/PHP as per your requirements. Make sure it tells users what they need to do next (where to click, whether to login, why the 404 page, etc). This will keep the visitors from leaving your website altogether. Place this file in your website’s root directory.

Here’s a sample 404 page (by Airbnb) for reference

airbnb 404 page

 

 

Update .htaccess file

Open .htaccess file of your Apache server in a text editor. You’ll find it in the root folder of website (e.g /var/www/html in Linux). If you can’t find it, just create a blank file named .htaccess in the root folder.

Add the following line to it. Replace new404.html with the filename of your 404 page.


ErrorDocument 404 /new404.html

The ErrorDocument directive tells Apache to redirect all “page not found” requests to your custom 404 page.

Remember that Apache looks for the custom 404 page in the root folder of your website. So if you have placed it in a sub folder (e.g /pages) you need to include it in the .htaccess file as shown


ErrorDocument 404 /pages/new404.html

 

Restart Apache Server

Restart your server to apply the changes


$ sudo service apache2 restart

 

That’s it! Now when users visit unavailable pages, they will be redirected to your custom 404 page.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!