[英]PHP Generate List of 301 redirects from CSV, and then Check List of 301 redirects for 404 errors
我今天有一項有趣的任務,在這個問題上找不到很多東西。 我想分享這一點,並就如何更好地完成此事征求任何建議。 我認為自己是一名平庸的程序員,他真的想提高自己的水平,因此,任何反饋都值得贊賞。 還有一個我找不到的奇怪錯誤。 因此,..希望這對曾經必須做類似事情的人有所幫助。
一位客戶正在重做網站,四處移動內容,並且需要進行數千次重定向。 市場營銷向我發送了一個XLS,其中一列中包含舊網址,下一列中包含新網址。 這些是我采取的措施:
編寫了一個腳本,該腳本:
然后,我將所有新指令復制/粘貼到我的.htaccess文件中。
然后,我編寫了另一個腳本,該腳本進行檢查以確保每個新鏈接都是有效的(無404s)。 第一個腳本完全按預期工作。 出於某種原因,我可以獲取第二個腳本來打印出所有404錯誤(有多個),但是該腳本在遍歷循環時不會消失,並且不會寫入文件,只是掛起在命令行中。 沒有錯誤得到報告。 知道發生了什么嗎? 這是兩個腳本的代碼:
格式301s:
<?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);
}
您應該已經使用fgetcsv()
生成了原始URL列表。 這會將CSV文件拆分為一個數組,從而簡化了轉換。
無法說出404錯誤或錯誤原因。 但是使用古怪的curl函數幾乎總是一個不好的指示。 為了進行測試,我會改用wget
類的命令行工具,以便可以手動驗證結果。
但是也許您可以嘗試使用PHP自己的get_headers()
。 它應該顯示原始結果標題; 不應該跟隨重定向本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.