function findBest($url) {
if(stristr($url,"http")) {
$p = parse_url($url);
$url = $p['path'];
}
$s = "sitemap.xml";
for($i=0;$i<10;$i++) {
if(file_exists($s)) { } else { $s = "../$s"; }
}
$data = file_get_contents($s);
$locs = getMultiMatch("<loc>","</loc>",$data);
foreach($locs as $loc) {
if(stristr($loc,"http:")) { $p = parse_url($loc); $loc = $p['path']; }
$l = levenshtein($url,$loc);
$results[$loc]=$l;
}
asort($results);
foreach($results as $k=>$v) {
$correct = $k;
$score = $v;
break;
}
if($score<3) {
return $correct;
} else {
return false;
}
}
function getSingleMatch($start,$end,$content) {
$exp = explode($start,$content);
$exp2 = explode($end,$exp[1]);
return $exp2[0];
}
function getMultiMatch($start,$end,$content) {
$exp = explode($start,$content);
for($i=1;$i< count ( $exp );$i++) {
$exp2 = explode($end,$exp[$i]);
$arr[] = $exp2[0];
}
return $arr;
}
$correct = findBest($_SERVER['REQUEST_URI']);
if($correct!=false) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: $correct");
exit;
}