Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Predictive 404

By: a guest on Oct 5th, 2011  |  syntax: PHP  |  size: 1.26 KB  |  views: 1,194  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function findBest($url) {
  2.     if(stristr($url,"http")) {
  3.         $p = parse_url($url);
  4.         $url = $p['path'];
  5.     }    
  6.    
  7.     $s = "sitemap.xml";
  8.     for($i=0;$i<10;$i++) {
  9.         if(file_exists($s)) { } else { $s = "../$s"; }
  10.     }
  11.  
  12.     $data = file_get_contents($s);
  13.     $locs = getMultiMatch("<loc>","</loc>",$data);
  14.     foreach($locs as $loc) {
  15.         if(stristr($loc,"http:")) { $p = parse_url($loc); $loc = $p['path']; }
  16.         $l = levenshtein($url,$loc);
  17.         $results[$loc]=$l;
  18.     }    
  19.     asort($results);
  20.     foreach($results as $k=>$v) {
  21.         $correct = $k;
  22.         $score = $v;
  23.         break;
  24.     }
  25.     if($score<3) {
  26.         return $correct;
  27.     } else {
  28.         return false;
  29.     }
  30. }
  31.  
  32. function getSingleMatch($start,$end,$content) {
  33.     $exp = explode($start,$content);
  34.     $exp2 = explode($end,$exp[1]);
  35.     return $exp2[0];
  36. }
  37.                        
  38.                        
  39. function getMultiMatch($start,$end,$content) {
  40.    $exp = explode($start,$content);
  41.    for($i=1;$i< count ( $exp );$i++) {
  42.        $exp2 = explode($end,$exp[$i]);
  43.        $arr[] = $exp2[0];
  44.    }
  45.    return $arr;
  46. }      
  47.  
  48. $correct = findBest($_SERVER['REQUEST_URI']);
  49.  
  50. if($correct!=false) {
  51.     header("HTTP/1.1 301 Moved Permanently");
  52.     header("Location: $correct");
  53.     exit;
  54. }
  55.