301 redirect not seen by googlebot

301 redirect not seen by googlebot - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the 301 redirect not seen by googlebot error on your web browser. Problem :


I have made a function on a wordpress site.



The functions purpose is the following.



1) if the post is not found, redirect to the archive of the post.



Should be fairly easy right?



Well - according to all sorts of tools, it isn't.



I start by creating this function:



function check_database_for_existance() 
if (is_main_query() && is_404() && !is_archive()){
//Do all sorts of magic here to find the correct link.

$redirectLink = "found-by-wp_magical-conditions";

header("HTTP/1.1 301 Moved Permanently"); //Or temporery, doesn't matter - doesn't work
header("Location: ".$redirectLink);
}


add_action('pre_get_posts', 'check_database_for_existance');


Now.. All tools I have for testing, suggest that it basically works wonderfully, everything from Toolbot to playstation3 understand the redirects fine.



But Googlebot (and other searchbots) refuses to accept it, and still sees the 404 page.



I have to use the pre_get_post hook - since i need to find out, if the posts exist, right?



Is there any way of forcing google to follow the redirect?



Am I redirecting to late in the process?



The documentation on Googlebots results are not very well documented IMO.



I have tested on : http://www.redirect-checker.org/index.php and using the ScreamingFrog program



I am at the ropes here - simply can not understand, why google won't follow the redirects.


Solution :

After your header calls you need to exit or die. If you don't the script continues on as usual which likely explains why you are seeing the results you are.



function check_database_for_existance() 
if (is_main_query() && is_404() && !is_archive()){
//Do all sorts of magic here to find the correct link.
$redirectLink = "found-by-wp_magical-conditions";
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$redirectLink);
die; // we are done here, nothing more to do
}



We hope that this article has helped you resolve the seo, redirects, wordpress error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

Redirected urls show in serp, with the original permalink but with the title and meta of the target page

How can I redirect everything but the index as 410?

How do I redirect traffic only if being accessed from a specific port?