When a web server gets a request, it tries to respond to the request, generally by delivering documents (that involves HTML pages), or by accessing the application with output in return (in case of other web apps and Content Management System).
An error is generated if anything goes wrong at this step, and there are unique codes (re: numbres) for each different type of error. You’re probably familiar with the most common 404 error that displays when a server cannot find a document.
If the error handling type is not specified by you then the browser will simply get the message from the server, and a generic message will be displayed by the browser. But this is not ideal.
Creating a custom error page is always beneficial as it shows a friendly and customized error message to your visitors if any URL of your website is not working. Thus, you can avoid those annoying ‘404 error that shows File Not Found’ and show an custom error, guiding your visitors back to your website content and explaining the possible solutions, rather than feeling them lost and frustrated. Here’s an example of a fun and cute 404 page from Lego, with a Lego man and his motto:
For setting customized error documents, you’ll need to make .htaccess file by following the guidance and the main instructions that included below.
ErrorDocument 404 /error_page/404.html
Lines above will tell Apache Web Server for showing the document which is located at the address /error_page/404.html (that’s under your website address/domain name) when a 404 error happens.
In the above example, it is assumed that the error document is created by you and named it as ‘404.html’ and you placed it within your domain in a directory named ‘error_page’. Example, it will be like https://www.yourdomianname.com/error_page/404.html
Like any other HTML document, the 404.html also is a normal document on your website and it can show any content you wish it to display, however, it is recommended to include a message i.e, ‘File Not Found’.
For setting up more error documents such as ‘403 Forbidden’, ‘500 Internal Server’, and ‘401 Unauthorized’ error message, you need to make a .htaccess file by following the guidance and the instructions including the text below.
ErrorDocument 401 /error_pages/401.html ErrorDocument 404 /error_pages/404.html ErrorDocument 500 /error_pages/500.html
It’s always good to show a custom error message to your users but it’s more important to resolve such type of errors. Instead of static HTML documents, use CGI script as error document for recording the errors in the database and do their resolution.
Again, if this sounds complex don’t worry: setting up a custom 404 nowadays is easy thanks to the availability of various pre-made solutions which show the errors that we get most frequently. Some examples are HotScripts.com and the CGI Resource Index.