How Many Redirects Will GoogleBot Follow?

Jun 30, 2009 by

It was a simple question for which I did not have an answer. Luckily, finding this answer was easy enough.

  1. Create a single 301 redirecting PHP file.
  2. Make this PHP file grab the current REQUEST_URI, then use an MD5 hash of that REQUEST_URI to create a new URL to which it will 301 redirect (we do this so that if and when Google comes back, it always finds the same redirects.
  3. Create a writable file that will record every time GoogleBot performs a redirect on the file
  4. Observe

Sure, it is a useless fact, but from our experiments GoogleBot will follow the redirect 5 to 6 times, and then give up. Never more than 6, never fewer than 5. Not sure if it is PageRank dependent at this point, will look into that later.

<?php

$uri = $_SERVER['REQUEST_URI'];
$exp = explode("/",$uri);
$wrd = $exp[count($exp)-1];

$newwrd = substr(md5($wrd),0,5);

$ua = $_SERVER['HTTP_USER_AGENT'];
if(stristr($ua,'google')) {
$ip = $_SERVER['REMOTE_ADDR'];
$fileloc = "/path/to/your/writable/file";
$old = file_get_contents($fileloc);
$fp = fopen($fileloc,"w");
fwrite($fp,$old."\n'$ip','$wrd','$newwrd','".date("YmdHis")."','$ua'");
fclose($fp);
sleep(3);
header("HTTP/1.1 301 Moved Permanently");
header("Location: 301experiment.php/$newwrd");

} else {
echo "301experiment.php/$newwrd";
}

?>