2011-01-26 55 views
1

我今天做了一件有趣的事情,在主题上找不到太多东西。 我想分享一下,并就如何更优雅地完成这件事提出任何建议。我认为自己是一个真正想要改进的平庸的程序员,所以任何反馈都非常感谢。还有一个奇怪的错误,我不明白。所以在这里..并希望这可以帮助谁曾经做过类似的人。PHP从CSV生成301重定向列表,然后检查301重定向404列表错误的列表

一位客户正在重做一个网站,移动内容,并且需要做几千次重定向。市场营销部门向我发送了一个包含一列中的旧网址的XLS,以及下一个新网址。这些人的行动我把:

  • 保存的XLS为CSV

写了一个脚本:

  • 格式化列表,有效301重定向
  • 导出列表到文本文件

然后我复制/粘贴所有新的指令我nto我的.htaccess文件。

然后,我写了另一个脚本,检查以确保每个新链接都是有效的(没有404s)。第一个脚本完全按照预期工作。 出于某种原因,我可以得到第二个脚本来打印出所有404错误(有几个错误),但是脚本在遍历循环时不会死亡,并且它不会写入文件,它只是挂在命令行中。没有错误报告。任何想法发生了什么?下面是两个脚本代码:

格式化301:

<?php 
$source = "301.csv"; 
$output = "301.txt"; 

//grab the contents of the source file as an array, prepare the output file for writing 
$sourceArray = file($source); 
$handleOutput = fopen($output, "w"); 

//Set the strings we want to replace in an array. The first array are the original lines and the second are the strings to be replaced 
$originalLines = array(
    'http://hipaasecurityassessment.com', 
    ',' 
); 
$replacementStrings = array(
    '', 
    ' ' 
); 

//Split each item from the array into two strings, one which occurs before the comma and the other which occurs after 
function setContent($sourceArray, $originalLines = array(), $replacementStrings = array()){ 
    $outputArray = array(); 
    $text = 'redirect 301 '; 
    foreach ($sourceArray as $number => $item){ 
     $pattern = '/[,]/'; 
     $item = preg_split($pattern, $item); 
     $item = array(
      $item[0], 
      preg_replace('#"#', '', $item[1]) 
     ); 
     $item = implode(' ', $item); 
     $item = str_replace($originalLines, $replacementStrings, $item); 
     array_push($outputArray,$text,$item); 
    } 
    $outputString = implode('', $outputArray); 
    return $outputString; 
} 


//Invoke the set content function 
$outputString = setContent($sourceArray, $originalLines, $replacementStrings); 

//Finally, write to the text file! 
fwrite($handleOutput, $outputString); 

检查404:

<?php 
$source = "301.txt"; 
$output = "print404.txt"; 

//grab the contents of the source file as an array, prepare the output file for writing 
$sourceArray = file($source); 
$handleOutput = fopen($output, "w"); 

//Split each item from the array into two strings, one which occurs before the space and the other which occurs after 
function getUrls($sourceArray = array()){ 
    $outputArray = array(); 
    foreach ($sourceArray as $number => $item){ 
     $item = str_replace('redirect 301', '', $item); 
     $pattern = '#[ ]+#'; 
     $item = preg_split($pattern, $item); 
     $item = array(
      $item[0], 
      $item[1], 
      $item[2] 
     ); 
     array_push($outputArray, $item[2]); 
    } 
    return $outputArray; 
} 

//Check each URL for a 404 error via a curl request 
function check404($url = array(), $handleOutput){ 

    $handle = curl_init($url); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

    $content = curl_exec($handle); 
    $response = curl_getinfo($handle); 

    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
    if($httpCode == 404) { 
     //fwrite($handleOutput, $url); 
     print $url; 
    } 
}; 


$outputArray = getUrls($sourceArray); 

foreach ($outputArray as $url) 
{ 
    $errors = check404($url, $handleOutput); 
} 
+0

与你的bug没有密切关系,但是当你有最终的重定向列表时,你应该看一下rewriteMap并且构建一个重定向的散列文件而不是写成千上万的重写规则,它会快得多。 – regilero 2011-01-26 20:08:42

+0

谢谢,伙计!我会给你一个镜头。 – 2011-01-27 17:02:07

回答

1

你应该使用fgetcsv()产生的原始URL列表。这将CSV文件分割成一个数组,简化了转换。

不能说404或错误原因。但使用古怪的卷曲函数几乎总是一个不好的指标。出于测试目的,我会使用命令行工具,如wget,以便可以手动验证结果。

但也许你可以尝试PHP自己的get_headers()来代替。它应该显示原始结果标题;不应该不遵循重定向本身。