Simply and dirty SEO SessionID Killer in PHP
Here’s a littlte snippet to kill SessionIDs for all results Googlebot and his companias like Yahoo SLURP! or ia_archiver fishes.
Point is, that if you generate SessionIDs in the URIs of your site, robots will fetch each page as new page generating duplicate content in the database of the search engine which results in bad ranking.
Here’s the code:
$sessionName = session_name();
$uri = $_SERVER[‘REQUEST_URI’];
if (preg_match("/&{$sessionName}=[a-z0-9]+/ims", $uri)) {
$uri = preg_replace("/&{$sessionName}=[a-z0-9]+/ims", ”, $uri);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $uri");
exit(0);
}
function seo_sid_callback($buffer) {
return preg_replace("/(&|&)".session_name()."=[a-z0-9]+/ims", ”, $buffer);
}
ob_start("seo_sid_callback");
}
Make sure you don’t post this snippet inside a ob_start()-Block! It has to be top-level. Here’s a better way to check for spiders – just in case you want to write something more sophisticated
Yes, i had one of these old sites back from the time when MacOS9 browsers – namely Internet Explorer Mac – had problems submitting cookies for every page, or didn’t get cookies set right (although the browser’slayout engine worked better than that on windows).
At least nowadays i haven’t encountered user problems using transient session ids.