Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3802858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:16:24+00:00 2026-05-19T14:16:24+00:00

I had an interesting task today and couldn’t find much on the subject. I

  • 0

I had an interesting task today and couldn’t find much on the subject. I wanted to share this, and ask for any suggestions on how this could have been done more elegantly. I consider myself a mediocre programmer who really wants to improve so any feedback is highly appreciated. There is also a strange bug I can’t figure out. So here goes..and hopefully this helps someone who ever has to do something similar.

A client was redoing a site, moving content around, and had a couple thousand redirects that needed to be made. Marketing sent me an XLS with old URLs in one column, new URLs in the next. These were the actions I took:

  • Saved the XLS as CSV

Wrote a script which:

  • Formatted the list as valid 301 redirects
  • Exported the list to a text file

I then copy / pasted all the new directives into my .htaccess file.

Then, I wrote another script that checked to make sure each of the new links was valid (no 404s). The first script worked exactly as expected. For some reason, I can get the second script to print out all the 404 errors (there were several), but the script doesn’t die when it finishes traversing the loop, and it doesn’t write to the file, it just hangs in command line. No errors get reported. Any idea what’s going on? Here is the code for both scripts:

Formatting 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);

Checking for 404s:

<?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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:16:25+00:00Added an answer on May 19, 2026 at 2:16 pm

    You should have used fgetcsv() for generating the original URL list. This splits up CSV files into an array, simplifying the transformation.

    Can’t say anything about the 404s or the error cause. But using the wacky curl functions is almost always a bad indicator. For testing purposes I would have used a commandline tool like wget instead so the results can be proof-checked manually.

    But maybe you could try PHPs own get_headers() instead. It’s supposed to show the raw result headers; shouldn’t not follow redirects itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had an interesting error today and couldn't find anything online about it so wondered
Had this interesting question being asked today and the arguments varied from Proxy to
Do you know any open software projects that had particularly interesting / well written
Today at work I had an interesting discussion with one of my coworkers. He
I have quite an interesting task at work - I need to find out
I had an interesting problem today. As part of practice with my fluency with
Had an interesting experience with Python's file buffering and wanted to know that I
I just had an interesting exchange about using .Except versus .Any, and the performance
I had an interesting request from a client today and I'm not exactly sure
I had this conversation with a colleague, and it turned out to be interesting.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.