Website Migration Tips and Tricks

This is a general resource post I’m building that will contain all the little tips and tricks and scripts I ran into or used while doing a website migration for my current employer. I hope the scripts and tips I post here are helpful.

I recently had to migrate an entire website, about 1,900 pages, from one domain to the next. All these pages were written in standard HTML and many links/URLs were absolute coded thanks to Adobe Contribute setup. This posed problems as the entire website, every single page needed the links properly changed. I wrote the below script To help speed up that process and am making it available to the public. It simply starts scanning the current directory and all sub-directories. It opens all TXT, HTML, and PHP files, scans them for the search terms and does a literal replacement with the replace term. The updated file is then save in place while the previous file is save with it’s same name as before but with an added timestamp for easy restoring. The script stores all changes to the log file for you review and gives total files/directories scanned and how many were changed. I used this to find all links and then changed my search and replace terms to find E-Mail addresses as well.

<?php
/*
 * Link Changer
 * 
 * Written to scan a directory tree from the current location of the file.
 * Scans each file and replaces the given search term with the replacement.
 * Logs all the files changed and makes a backup copy prior to editing.
 *
 * Daniel Moree - February 19th, 2015
 */

// Search terms
// These should start from most complex to most simple to ensure nothing is improperly changed.
$search_term = array("https://www.sapc.edu", "http://www.sapc.edu", "https://sapc.edu", "http://sapc.edu", "www.sapc.edu");

// Replace term
$replace_term = "https://www.sa.edu";

// Log file name
$log_filename = "linkchanger.log";

// Error reporting level
error_reporting(E_ERROR);

// Test Mode
$test_mode = true;

////////////////////////////////////////
// DO NOT CHANGE BELOW HERE
////////////////////////////////////////
$file_log = array();
$changed_count = $file_count = $dir_count = 0;

scan_directory(".");
file_put_contents($log_filename, "Scanned $dir_count directories and $file_count files and changed $changed_count files\r\n");
file_put_contents($log_filename, implode("\r\n", $file_log), FILE_APPEND);
echo "Log file written to: ".$log_filename."\n";
echo "\n\nScanned $dir_count directories and $file_count files and changed $changed_count files\n";

function scan_directory($the_directory){
        global $dir_count;
        $dir_count++;

		echo $the_directory."\n";
		array_push($file_log, $the_directory);
		
        $dir = dir($the_directory);
        while(($file = $dir->read()) !== false){
                if($file != ".." && $file != "." && $file != basename(__FILE__)){
                        if(is_dir($the_directory."/".$file) == true && !is_link($the_directory."/".$file)){
                                scan_directory($the_directory."/".$file);
                        }elseif(is_file($the_directory."/".$file) && (substr($file, -4) == ".php" || substr($file, -4) == ".txt" || substr($file, -5) == ".html")){
                                parse_file($the_directory."/".$file);
                        }
                }
        }
}

function parse_file($file){
        global $file_log, $file_count, $search_term, $replace_term, $test_mode,$changed_count;

        if(substr($file, -4) != ".php"){
                return;
        }

        $file_count++;

        $count = 0;
        $file_contents = file_get_contents($file);

        $new_file_contents = str_replace($search_term, $replace_term, $file_contents, $count);
        if($count > 0){
                $changed_count++;
                echo "Replaced strings in: ".$file."\n";
                array_push($file_log, $file." : ".$count." replacements");
                if(!$test_mode){
                        rename($file, $file."-".date('Ymd'));
                        file_put_contents($file, $new_file_contents);
                }
        }

        return;
}
Back To Top