Skip to content

Instantly share code, notes, and snippets.

@citizen428
Created September 26, 2011 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save citizen428/1241762 to your computer and use it in GitHub Desktop.
Save citizen428/1241762 to your computer and use it in GitHub Desktop.
Given a WP XML export, this script will generate the necessary 301 redirect rules for all posts for Apache or Nginx.
require 'date'
require 'rexml/document'
include REXML
doc = Document.new(File.new(ARGV[0]))
format = ARGV[1] || "apache"
formats = {
:apache => "RewriteRule ^%s$ /%s [R=301,L]",
:nginx => "rewrite ^%s /%s permanent;"
}
File.open("redirects.txt", "w") do |f|
doc.elements.each("rss/channel/item[wp:status = 'publish' and wp:post_type = 'post']") do |e|
post = e.elements
date = DateTime.parse(post['wp:post_date'].text)
slug = post['wp:post_name'].text
post['link'].text =~ %r{.*(/archives/\d+)}
old_url = $1
new_url = "blog/#{date.strftime("%Y/%m/%d")}/#{slug}/"
f.puts formats[format.intern] % [old_url, new_url]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment