How to Define Custom Error Page Using .htaccess
Custom error pages are very useful as it shows a friendly message to the visitor in case of any error occurs at your server end. For example if the visitor is trying to browse a URL which is not present in your website then it will show a custom 404 error page which will contain more information. So how do you define custom error page using .htaccess file configuration. Let’s see how we can define different custom error pages using .htacces configuration. (In case you want to know What Is .htaccess File)
How to Set Custom 404 Page Not Found Error Page
Defining a custom 404 error page for your website is extremely easy if you are using apache webserver or if your hosting provider supports the .htaccess file configuration. All you need to do is create a .htaccess file under the root directory of your website or if the file is already there under the root directory you can modify it and add the below lines of code to it.
#Define Custom 404 error page
ErrorDocument 404 /error/404.html
Above configuration tells the server that if a 404 error occurs at server then show the /error/404.html page to the visitor. Make sure that you have already created the 404.html file under the directory error and the location of the directory should be immediately inside the root directory. You can give any name to the directory and file, it is purely up to you.
How to Define Different Error Documents in .htaccess
In the earlier example of custom 404 page we have seen the format for defining an error document. So let’s analyze the format.
The first part of the configuration is ErrorDocument which tell that this configuration is for defining error document.
The second part is the type of error in earlier case it was 404, so in the second part you need to write the html error code, 404, 500, 401 etc. The third part of the configuration is the document path.
So now you can define any custom error document with this format. The general format for defining the custom error document is:
ErrorDocument html-error-code document-path
So here you just need to replace html-error-code with the exact error code and document-path with the relative path to your custom error page. Hope this tutorial was useful for you. I will continue the tutorial of .htaccess configurations in the next part.