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:
## Redirecionar em caso de erros ##
ErrorDocument 400 /apache.php?erro=400
ErrorDocument 403 /apache.php?erro=403
ErrorDocument 404 /apache.php?erro=404
ErrorDocument 500 /apache.php?erro=500
ErrorDocument 501 /apache.php?erro=501
ErrorDocument 502 /apache.php?erro=502
ErrorDocument 503 /apache.php?erro=503
ErrorDocument 504 /apache.php?erro=504
ErrorDocument 505 /apache.php?erro=505
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.