Porting Web Content Easily


Recently, I moved my web content to another server. One would think this would be a trivial task. At first, it was. The basic steps I initially took were: All pages were written with relative links, so I had no trouble with stale link errors. At this point, I thought that was it. However, another important issue was: what about redirection? It seemed that alot of people had bookmarked many parts of my site under the past server. Putting a redirection on the home page was simply not good enough if I wanted these people to continue to access my content with as little hassle as possible.

My plan at this point was to write a script which would:

So, when users access any HTML content, either by bookmark, or search engine results, a redirector file would be waiting to take them to the new page. Here's how the script works:

As a result, whenever a user seeks a page on my old address, they get redirected to the new address to the same page under that tree.

     1	#!/usr/bin/perl -lw
     2	
     3	use strict;
     4	use File::Find ();
     5	use File::Copy;
     6	use vars qw($name);
     7	
     8	my $old_server_str = "http://chat.carleton.ca/~tkralidi";
     9	my $new_server_str = "http://www.kralidis.ca";
    10	my @files;
    11	my $count = 0;
    12	
    13	# find HTML files
    14	@ARGV = qw(.) unless @ARGV;
    15	sub find(&@) { &File::Find::find }
    16	*name = *File::Find::name;
    17	find { push @files, $name if /^.*(htm|html|shtml)$/ && -T } @ARGV;
    18	
    19	foreach(@files) {
    20	  # trim and make redirector basename / if file is index.*htm* (don't show index.*htm*)
    21	  my $trans_str;
    22	  chomp;
    23	  s#^./##;
    24	  if ($_ =~ /(.*)index.(htm|html|shtml)$/) {
    25	    $trans_str = $1;
    26	  }
    27	  else {
    28	    $trans_str = "$_";
    29	  }
    30	 
    31	  # write out page template 
    32	  (my $page = <<"  ENDHTML") =~ s/^\s+//gm;
    33	  <HEAD>
    34	  <TITLE>This page has moved</TITLE>
    35	  <META HTTP-EQUIV="REFRESH" CONTENT="5; url=$new_server_str/$trans_str">
    36	  </HEAD>
    37	
    38	  <BODY>
    39	
    40	  <HR NOSHADE>
    41	
    42	  <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    43	  <!--
    44	  document.write("This page (" + document.URL + ")");
    45	  //-->
    46	  </SCRIPT>
    47	  <P>
    48	  has been moved to:
    49	  <P>
    50	  <A HREF="$new_server_str/$trans_str">$new_server_str/$trans_str</A>
    51	  <P>
    52	  You will be redirected in 5 seconds
    53	  <P>
    54	  Please update your bookmarks/favourites if you have bookmarked this site.
    55	  <HR NOSHADE>
    56	
    57	  </BODY>
    58	  </HTML>
    59	
    60	  ENDHTML
    61	
    62	  # copy original HTML file to .orig and write out new file w/ template contents
    63	  copy("$_", "$_.orig") or die "$_ : $!\n";
    64	  open(OUT, ">$_") or die "$_ : $!\n";
    65	  print OUT "$page";
    66	  close(OUT);
    67	  print "$_ processed";
    68	  $count++;
    69	}
    70	
    71	print "$count files processed";
    72	
    73	exit(0);
  

January 2001