Moving a WordPress blog

Since I’m preparing to release a new app next week, I wanted to make my MC Development site focus on software and services, so I moved my personal blog off that site. One major goal was to maintain all of my existing posts and preserve all of the old permalinks, which I accomplished.

Here’s how I did it.

The first step was to do a clean install of WordPress on this site. Meanwhile, I went to my old site and exported all content at /wp-admin/export.php. When it finished exporting, I returned to this site and imported the XML file from the last step at /wp-admin/import.php. Since the file was huge, the import took several minutes. When it finished, all of the posts, pages, and comments were intact. Finally, I had to make sure I installed & enabled all of the appropriate plugins at this site.

To preserve the old permalinks, I had to add a few rewrite rules to the old site. Since I’m using nginx, I added the following lines to my nginx.conf file:

# redirect permalinks to blog.mcohen.me

rewrite ^/blog/ http://blog.mcohen.me/ last;

# all other requests go to WordPress
    if (!-e $request_filename) {
	  rewrite ^(/20../../../.*)$ http://blog.mcohen.me/$1 last;
      rewrite ^.*$ /index.php last;
}

location ~ /\.ht {
    deny  all;
}

The first rewrite rule does a simple redirect of http://example.com/blog to this site.

The second rule is a bit tricky. I’m using permalinks of the form /%year%/%monthnum%/%day%/%postname%/ and I wanted to redirect only those permalinks and nothing else to this site. The first part of the rule, ^(/20../../../.*)$ matches 4 characters starting with 20 (since my earliest posts are in 2002), followed by a slash, followed by two more characters, another slash, two more characters, another slash, and any number of characters. The second part of the rule creates a URL at this site using the matched characters.

The final rewrite rule simply passes anything else to WordPress.

If you’re running Apache, you would have to use similar rules in your .htaccess.

Leave a Reply