Return to Snippet

Revision: 43212
at March 19, 2011 05:00 by nielsenrc


Initial Code
<?php

//Set Options

        $old_url_text_file = "old_urls.txt";
        $base_url = "http://www.premierfoot.com/";
        $replacement_base_url = "http://www.premierfoot.com/"; //Make Sure to Add Trailing Back Slash

        $new_url_text_file = "new_urls.txt";
        $redirect_text_file = "new_urls.txt";
        $default_redirection_page = "index.html";

//Create Array of Old Urls from Text File Contents

        $old_url_list = file_get_contents($old_url_text_file);
        $old_urls = explode("\n", $old_url_list);
        $old_urls = array_filter(array_map('trim', $old_urls));

//Create Array of New Urls from Text File Contents

//        $new_url_list =  file_get_contents($new_url_text_file);
//        $new_urls = explode("\n", $new_url_list);
//        $new_urls = array_filter(array_map('trim', $new_urls));

//Print RewriteBase Outside of Loop
print "RewriteBase /" . "<br />";

//Loop Through Urls and Generate 301 Redirect Syntax

foreach ($old_urls as $old_url) {
    
    $old_url = str_replace($base_url, "", $old_url);

     if($old_url !== "/") {

         print "RewriteRule ^" . $old_url . "$" . " " . $replacement_base_url . $default_redirection_page . " " . "[R=301,L]" . "<br />";

     }
}
?>

Initial URL

                                

Initial Description

                                

Initial Title
Create 301 Redirect Template from Text File

Initial Tags
php

Initial Language
PHP