How to Create Custom Error Pages with htaccess?

This article gives you a step by step guide that will help you to create custom error pages. It is easy to implement and to understand

How to Create Custom Error Pages with htaccess?
Some months ago, I moved some of my websites from subdomains to the main domain for better management. For some reason, I forgot about the HTTP error status that will appear when visitors try to access the site. As a result, my Google Search Console account sent me an email with a list of 404 and 5xx errors to fix. I had to create a redirect to avoid losing my visitors. More often, when a website redirects you to the HTTP error status code, the default page will display a blank screen with the server error message. In such cases, the page does look professional, and the website owner is losing potential customers. In this article, we will learn together how to create custom Error Pages with the .htaccess. Additionally, I will show how I redirected all the requests from Search Engines to the new domain.

Custom Error Pages with .htaccess

There are many HTTP error status codes that your server can send. Most of the common ones are 400, 401, 403, 404,  500,  502 and 504.
  • 400 error code indicates that the server was enabled to process the request due to incorrect syntax
  • 401 Error code means that the requested URL can not proceed due to the lack of valid authentication credentials because of unauthorized access.
  • 403 forbidden error indicates that access to the client is not allowed to access the URL
  • 404 Not Found means that the browser was able to communicate with the server, but the server couldn't find the URL.
  • 500 Server Error is a general HTTP status code that means something is wrong with the request, but the server is not specific about the error.
  • The 502  error means that the server received an invalid response from another server.
  • Finally, The 504 Gateway Timeout error means that the server did not receive a timely response from another server. You can read an excellent article about the 504 Gateway on Lifewire. They speak deeply about that and give tips to fix it.
Now that we briefly defined that HTTP error, if you have any of that, this is what you should do:
  • First of all, you access your server files
  • Secondly, in the root directory, you can use the existing .htaccess file or create one (if you don't have)
  • Thirdly, you will add the below line of code to your .htaccess file.
Note that you can customize the HTML files as you want. For instance, if you have some basics HTML skills, you can create an excellent design to explain to your visitors what to do. For example, you can add links to your web pages.

Redirect all the requests from Search Engines to the new domain name.

The method I use to redirect all the requests from the previous subdomain to the new domain is not necessarily conventional. Nevertheless, it is a temporary fix to avoid me losing all my visitors. To redirect, this is what I did.
  • First of all, I created an index.php page
  • Secondly, I added this  javascript redirect code
  • Thirdly, in the .htaccess file, I added a rewrite that will handle all the request as a query with the GET method. As such, the index.php will receive the links and redirect the user to the new website.
There are many alternatives to that. You can, for example, add a not for your visitor to the error file and let them know that the link doesn't exist and ask them to click for the right one. Thanks for reading. Please do not hesitate to comment and give your feedback. We are also available if you need any website and eCommerce services.

What's Your Reaction?