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 ensure that for each HTTP error code, you have control over how you want to handle it. When the user types an invalid or non-existent 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, personalization and visual art so that the visitor reads a friendly message informing that the page does not exist and suggesting other URLs, using the layout of my website and several links available for him to browse. 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.
How can I direct an error page to the main page of the site, for example, to the link http://www.meusite.com.br?
Just to reinforce ...
I use WordPress and my pages do not have an extension, for example, "mypage.php" or "index.php" or "index.html" ... the link is direct, so ... http://www.meusite.com.br/blog/sobre
Since WordPress htaccess already comes with the necessary changes to 404 error handling, you can edit the 404.php file, which is in the theme folder you are using (if there is no such file, you can create a new one) and replace the content with this code:
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . get_bloginfo('url'));
Another alternative is to use a plugin for this, such as Redirection (https://wordpress.org/plugins/redirection/), which allows you to do this redirection without any knowledge of htaccess.
Hello, how do I redirect all pages with error 404 to my home page?
Just use this code in your .htaccess file:
ErrorDocument 404 /suapagina.php
Hello, I have a problem.
If the user types http://www.site.com.br/pagina.php targeting the error page works fine, but as we use friendly URL when typing http://www.site.com.br/pagina don't drive anywhere How should I proceed in this case?
I created a post that should help you .. See the link http://www.dirceuresende.com/blog/implementando-url-amigavel-no-apache-com-htaccess-e-php/
If you still have questions, let me know that I will help you.