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 .htaccessThere 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.
- 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.
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
- 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.