Overview
Custom error pages enable you to customize the pages that are displayed when an error occurs. Not only do they make your website more professional, they can also save you from losing visits to your site. If a visitor sees a generic error page, they are likely to leave your site. However, if they see a helpful error page, they may continue to stay because they can simply click a link to go to another page within your site.
The following instructions are for an Apache server. If you're running Nginx, jump to the section at the bottom titled Creating custom error pages with Nginx.
A note on 500 errors
Due to how Apache is configured, it's not possible to redirect to a custom 500 error page. When a 500 error is encountered, the browser default 500 page will display.
Simple configuration
To create a custom error page for your domain, add the following line to an .htaccess file located in your domain’s web directory:
ErrorDocument 404 /error.php
This redirects browsers that experience a 404 error (Not Found) to the file error.php located in the same directory as your .htaccess file. Alternatively, you can use the above line as a template to create separate custom error pages for each error.
Status codes
The complete (and very long) list of errors is available here:
Some of the most common errors you'll probably want to make entries for are:
- 400 – Bad Request
- 401 – Unauthorized
- 403 – Forbidden
- 404 – Not Found
For example, to catch those errors, you would add the following to your .htaccess file for the domain you'd like to configure:
ErrorDocument 400 /error.php ErrorDocument 401 /error.php ErrorDocument 403 /error.php ErrorDocument 404 /error.php
This forces a browser to redirect to the /error.php file if it encounters any of the status codes above.
Setting up the error.php file
Next, in error.php, add something similar to the following. This particular example is made for a wiki site: if someone visits https://www.example.com/Foo, then they are redirected to https://www.example.com/wiki/Foo. Anything after the last "/" is assumed to be a wiki article they are trying to reach:
<?php $page_redirected_from = $_SERVER['REQUEST_URI']; // this is especially useful with error 404 to indicate the missing page. $server_url = "http://" . $_SERVER["SERVER_NAME"] . "/"; $redirect_url = $_SERVER["REDIRECT_URL"]; $redirect_url_array = parse_url($redirect_url); $end_of_path = strrchr($redirect_url_array["path"], "/"); switch(getenv("REDIRECT_STATUS")) { # "400 - Bad Request" case 400: $error_code = "400 - Bad Request"; $explanation = "The syntax of the URL submitted by your browser could not be understood. Please verify the address and try again."; $redirect_to = ""; break; # "401 - Unauthorized" case 401: $error_code = "401 - Unauthorized"; $explanation = "This section requires a password or is otherwise protected. If you feel you have reached this page in error, please return to the login page and try again, or contact the webmaster if you continue to have problems."; $redirect_to = ""; break; # "403 - Forbidden" case 403: $error_code = "403 - Forbidden"; $explanation = "This section requires a password or is otherwise protected. If you feel you have reached this page in error, please return to the login page and try again, or contact the webmaster if you continue to have problems."; $redirect_to = ""; break; # "404 - Not Found" case 404: $error_code = "404 - Not Found"; $explanation = "The requested resource '" . $page_redirected_from . "' could not be found on this server. Please verify the address and try again."; $redirect_to = $server_url . "wiki" . $end_of_path; break; } ?>
<!DOCTYPE html> <head> <link rel="Shortcut Icon" href="/favicon.ico" type="image/x-icon" /> <?php if ($redirect_to != "") { ?> <meta http-equiv="Refresh" content="5; url='<?php print($redirect_to); ?>'"> <?php } ?> <title>Page not found: <?php print ($redirect_to); ?></title> </head> <body> <h1>Error Code <?php print ($error_code); ?></h1> <p>The <a href="http://en.wikipedia.org/wiki/Uniform_resource_locator">URL</a> you requested was not found. <?PHP echo($explanation); ?></p> <p><strong>Did you mean to type <a href="<?php print ($redirect_to); ?>"><?php print ($redirect_to); ?></a>?</strong> You will be automatically redirected there in five seconds.</p> <p>You may also want to try starting from the home page: <a href="<?php print ($server_url); ?>"><?php print ($server_url); ?></a></p> <hr /> <p><i>A project of <a href="<?php print ($server_url); ?>"><?php print ($server_url); ?></a>.</i></p> </body> </html>
Style the page so that it fits with the rest of your site and you're done with basic error handling.
Creating custom error pages with Nginx
You can also create custom error pages if you're running Nginx instead of Apache.
- First, view the following article to understand where your config files should go:
- Create a file named error.conf in the following directory:
/home/username/nginx/example.com/error.conf
- In the error.conf file, add the following:
error_page 404 /404.html; location /404.html { internal; }
- Next, navigate to your site directory. For example:
/home/username/example.com
- In that directory create a page titled 404.html with any error message you wish to display.
- Restart your server (if on a VPS). If on Dedicated, you can reload Nginx yourself.
- If you now visit a page on your site that doesn't exist, your custom error page will display.
See also
Internal links
External links
- Apache Docs - Custom Errors Responses
- HTTP/1.1 Status Code Definitions (see specifically the 4xx and 5xx codes)