Posted By

nielsenrc on 03/19/11


Tagged

php


Versions (?)

Create 301 Redirect Template from Text File


 / Published in: PHP
 

  1. <?php
  2.  
  3. //Set Options
  4.  
  5. $old_url_text_file = "old_urls.txt";
  6. $base_url = "http://www.premierfoot.com/";
  7. $replacement_base_url = "http://www.premierfoot.com/"; //Make Sure to Add Trailing Back Slash
  8.  
  9. $new_url_text_file = "new_urls.txt";
  10. $redirect_text_file = "new_urls.txt";
  11. $default_redirection_page = "index.html";
  12.  
  13. //Create Array of Old Urls from Text File Contents
  14.  
  15. $old_url_list = file_get_contents($old_url_text_file);
  16. $old_urls = explode("\n", $old_url_list);
  17. $old_urls = array_filter(array_map('trim', $old_urls));
  18.  
  19. //Create Array of New Urls from Text File Contents
  20.  
  21. // $new_url_list = file_get_contents($new_url_text_file);
  22. // $new_urls = explode("\n", $new_url_list);
  23. // $new_urls = array_filter(array_map('trim', $new_urls));
  24.  
  25. //Print RewriteBase Outside of Loop
  26. print "RewriteBase /" . "<br />";
  27.  
  28. //Loop Through Urls and Generate 301 Redirect Syntax
  29.  
  30. foreach ($old_urls as $old_url) {
  31.  
  32. $old_url = str_replace($base_url, "", $old_url);
  33.  
  34. if($old_url !== "/") {
  35.  
  36. print "RewriteRule ^" . $old_url . "$" . " " . $replacement_base_url . $default_redirection_page . " " . "[R=301,L]" . "<br />";
  37.  
  38. }
  39. }
  40. ?>

Report this snippet  

You need to login to post a comment.