Did you know that Apache web server allows you to configure custom 404 error page for your website? Using ErrorDocument directive you can easily configure custom error page in Apache, without any coding. In this article, we will look at how to create custom 404 page in Apache. You can also use these steps to configure custom error pages for other error codes such as 403, 500, 502, etc.
How To Create Custom 404 Page in Apache
Here are the steps to configure custom 404 error page for your website. We will use the ErrorDocument directive to setup custom 404 page. Please make sure that you have your error page file ready, before you proceed further. We have assumed that our error file error404.html is located at website root folder /var/www/html/.
You can add ErrorDocument directive in .htaccess file, virtual host configuration file or your main server configuration file, as per your requirement.
1. Open .htaccess file
You will typically find .htaccess file in your site’s root folder (e.g /var/www/html/). You can open it using vi editor
$ sudo vim /var/www/html/.htaccess
If you have not setup .htaccess but have setup virtual hosts, then you can also open your virtual host configuration file (e.g /etc/apache2/sites-enabled/example.com for your domain example.com)
$ sudo vim /etc/apache2/sites-enabled/example.com
Alternatively, you can also open the default virtual host configuration file
$ sudo vim /etc/apache2/sites-enabled/000-default.com
Also read : How to Create Self-Signed Certificate for Apache
2. Configure 404 Error Page
Add the following line to .htaccess file
ErrorDocument 404 /error404.html
In the above line, we specify the error response code 404 and the custom page error404.html to be served for it. You can also configure error pages for other response codes such as 500, 502, 403, etc.
If you are using Virtual host file, add the above line inside VirtualHost tag
<VirtualHost> ... ErrorDocument 404 /error404.html ... </VirtualHost>
Also read : How to Use Apache bench for Load Testing
3. Create 404 Error page
Create custom error file error404.html using text editor, or a page builder like Squarespace, WordPress, or Wix.
4. Restart Apache Server
Restart Apache Server to apply changes
$ sudo service apache2 restart
Hopefully, this article will help you create custom 404 pages for your website. You can use these steps to configure custom pages for other error codes such as 500, 502, 403, etc.