Overview
This article explains how to redirect server errors to custom error pages on your website.
Background
Custom error pages enable you to customize the pages that display when an error occurs on your website. This creates a more professional and user-friendly experience and helps prevent visitors from leaving your site. For example, if a visitor sees a helpful error page, they are more likely to stay because they are able to simply click a link to go to another page within your site.
Creating the custom error page
You must manually create the page you redirect an error to. The contents of this file will be different depending on the programming language your website uses and how you would like the page to appear.
Additionally, some CMS software applications (like WordPress) may have a built-in page for you to redirect errors to. Check your website's software documentation to confirm if such a feature is available.
Apache
The following steps create a redirect to a custom error page on an Apache server.
Redirecting to an error page
These steps edit your website's .htaccess file to redirect a specific error code to a specific error page. In this example, 404 errors are redirected to a page titled error.php.
- Log into your server via SSH.
- Navigate to your website directory.
- Create a file named .htaccess.
- Add the following line to this file:
ErrorDocument 404 /error.php
You can use this example as a template to create custom error pages for each error code.
Status codes
See this page for a complete list of status codes. The list is very long, but there are only a few common errors you'll need to redirect, such as the following:
- 400 – Bad Request
- 401 – Unauthorized
- 403 – Forbidden
- 404 – Not Found
500 errors on an Apache server
Due to how DreamHost Apache servers are configured, it is not possible to redirect to a custom 500 error page. When a 500 error is encountered, the default browser 500 page will always display.
Nginx
The following steps create a redirect to a custom error page on an Nginx server.
The configuration files for Nginx sites are placed in a special directory under your username. See this article for further details on where to place your config file.
Redirecting to an error page
These steps edit your user's error.conf file to redirect a specific error code to a specific error page. In this example, 404 errors are redirected to a page titled 404.html.
- Log into your server via SSH.
- Navigate into the following directory.
Make sure to change username to your Shell user and example.com to your website.
[server]$ cd /home/username/nginx/example.com
- Create a file named error.conf in this directory. You can name this file anything you like, but it must use the extension .conf.
- Add the following code to this file:
error_page 404 /404.html; location /404.html { internal; }
- Navigate to your site directory.
[server]$ cd ~/example.com
- Create a page titled 404.html with any error message you wish to display.
- Reboot your server (if on a VPS). If on Dedicated, you can reload Nginx yourself.
When you now visit a page on your site that does not exist, your custom error page displays.