Ruby code for creating redirects for my (roughly) 100 old posts

As I mentioned in a previous post, I moved my blog from my own host, to Google's blogger. As a result, there are ~ 100 old blog posts that are linked to from all over the place and I'd like to have a graceful way to re-direct. The previous post mentioned updated the header with a 301 redirect. Going through so many old posts, and updating each one was nuts, so I wrote a small ruby script to handle the duties.

#!/usr/bin/ruby
# update_redirects.rb
# This script will traverse the directory given as the first argument
# and replace the contents of all files with the 'redirect_text'
#
# some special conditions: it assumes that they are php redirects, but you
# can use <meta> redirect tags as well for plain HTML
#
#

require 'find'

dir = ARGV[0]
redirect_text ='<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://blog.myhdbox.com/%%PATH%%");
exit();
?> '

# find all the files that were published by blogger (they are all in their
# respective year directories)
#
Find.find(dir) do |path|
if path=~/^\.\/200.*.php$/
f=File.open(path,'w') # open 'w' truncates the file
new_text= redirect_text.gsub("%%PATH%%",path.gsub("./",""))
f.write(new_text)
f.close()
end
end

0 comments: