Skip to content

Instantly share code, notes, and snippets.

@pavanpodila
Created May 24, 2012 15:44
Show Gist options
  • Save pavanpodila/2782304 to your computer and use it in GitHub Desktop.
Save pavanpodila/2782304 to your computer and use it in GitHub Desktop.
Script to generate Rewrite conditions for .htaccess
require 'rubygems'
require 'nokogiri'
require 'date'
def newUrl(baseUrl, date, name)
"#{baseUrl}/#{date.strftime '%Y/%m'}/#{name}"
end
def writeMap(items)
txtFile = File.open 'urlmap.txt', 'w'
items.each do |item|
postName = item.xpath('wp:post_name').inner_text()
postId = item.xpath('wp:post_id').inner_text()
postDate = Date.parse(item.xpath('wp:post_date').inner_text())
txtFile << "RewriteCond %{QUERY_STRING} ^p=#{postId}$\n"
txtFile << "RewriteRule ^$ #{newUrl('', postDate, postName)}? [R=301,L]\n\n"
end
end
def setLinks(items, doc)
items.each do |item|
item.xpath('link').first().inner_html = item.xpath('guid').inner_text()
end
File.open("links-fixed.xml", "w") { |f| doc.write_xml_to f }
end
file = "../pixelingene.wordpress.2011-09-16.xml"
doc = Nokogiri::XML(File.open file)
blogUrl = doc.xpath('rss/channel/wp:base_blog_url').inner_text()
items = doc.xpath '//item[wp:status="publish" and wp:post_type="post"]'
items = items.sort { |x, y|
d1 = Date.parse x.xpath('wp:post_date').inner_text()
d2 = Date.parse y.xpath('wp:post_date').inner_text()
d1 <=> d2
}
writeMap items
#setLinks items, doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment