How to use custom error pages in Apache
Custom error pages in Apache allow web administrators to provide a more user-friendly experience when a visitor encounters an error on the website. Instead of displaying generic error messages, custom pages can be designed to match the site's theme, provide helpful information, or direct users to other parts of the site.
Apache web server allows the configuration of custom error pages for various HTTP status codes like 404 (Not Found), 500 (Internal Server Error), and others. This customization enhances the user experience and can even retain visitors who might otherwise leave the site after encountering an error.
To configure custom error pages in Apache, you'll need the custom error pages you want to display saved in HTML or other web-compatible formats, and configure the Apache options accordingly.
Steps to configure custom error pages for Apache:
-
Launch terminal.
-
Open the Apache configuration file using your preferred text editor.
$ sudo vi /etc/apache2/sites-available/000-mysite.conf
-
Add the ErrorDocument directive followed by the path to the custom error page.
ErrorDocument 404 /custom_404.html
This could be within a specific <VirtualHost> block if you want the custom pages to apply to a particular site, or outside of those blocks to apply server-wide.
-
Repeat the above step for any other error codes and sites you want to customize.
-
Save the configuration file and exit the text editor.
-
Check the configuration syntax to ensure there are no errors.
$ sudo apache2ctl configtest
-
Restart the Apache service to apply the changes.
$ sudo systemctl restart apache2
-
Test the custom error pages by attempting to access a non-existent page or other error-triggering conditions on your site.