Rewriting Wordpress permalinks for Ghost with nginx

I migrated this blog to Ghost some time ago, but I thought it might be useful to someone else to see how I kept my permalinks intact after moving off Wordpress.

Here's the problem: Wordpress used this format for posts /year/month/post-title.html
Ghost, out of the box, uses simply /post-title which I like.

The Ghost Wordpress plugin worked great to do the initial import, and kept most of my fancy formatting. But since Cool URIs don't change I still needed some way to make the permalinks work. At the time I couldn't find a way to make Ghost use Wordpress-style permalinks and, honestly, it felt like too much structure for this tiny blog.

Turns out that the Wordpress XML export includes the permalinks for each post, so I wrote some XPath to extract just those elements, something like this:

xpath weblogfred.wordpress.2013-12-07.xml //item/wp:postmeta/wp:meta_value@text

The command-line xpath tool's output sucks, so I ended up copying-and-pasting the list of permalinks into Emacs.
This ended up with a list like:

/about
/2009/04/problems-installing-easy_install-python-imaging-library.html
/2009/06/the-unofficial-way-to-uninstall-darwinports-macports.html
...

With the help of a little Emacs macro magic, I rewrote the list to:

rewrite ^/about$ /about permanent;
rewrite ^/2009/06/the-unofficial-way-to-uninstall-darwinports-macports.html$ /the-unofficial-way-to-uninstall-darwinports-macports permanent;
...

I then saved those in /etc/nginx/permalinks.conf and then added include permalinks.conf; in the correct server block in my nginx config.

All in all, this worked pretty well, but was way more complicated than I'd like. At least it was a one time deal, and was right at the threshold of "number of things that I'll deal with manually" which works out to be about 30 items.

Here's the final result:

https://gist.github.com/phred/6dda1fd3f60eb0e5df6d