Created
September 26, 2011 07:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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