HTTP error redirection using .htaccess (Apache)

Views: 3.333
Reading Time: <1 minute

Good evening.

Today I will show you how to redirect your applications in case of a URL typo, for example, where the Apache web server usually displays an error message and the visitor may have difficulty returning to your site. If he found your site through a search engine (aka Google), he will probably leave your site and go back to Google. We can't let that happen, correct?

Once again, I will turn to our great ally, the Apache HTTP Server settings file, .htaccess to help us.

To create the redirect, let's use the directive ErrorDocument, as we can see in the example below:

This will make sure that for each HTTP error code you have control of how you want to handle it. When the user enters an invalid or nonexistent URL (Error 404 - Not Found), I am redirecting to the URL /apache.php?error=404. Inside the apache.php file I do the necessary treatments, customization and visual art for the visitor to read a friendly message informing that the page does not exist and suggesting other URL's, already using the layout of my site and several links available for him to navigate. usually.

Thus, we even conquered visitors coming from broken links or mistakes! Oh, do you need help identifying HTTP error codes? Learn more here.
To the next.