about:benjie

Random learnings and other thoughts from an unashamed geek

Moving Blog Software - Serendipity to Wordpress

| Comments

Screenshot Wordpress

I moved my blog (in fact my entire website!) over to Wordpress a couple of days ago. The move was not without it’s challenges - for a start I remembered Wordpress likes to have a well defined hostname, and I didn’t want any downtime. To get around this, I placed an entry in my /etc/hosts file for www.benjiegillam.com, pointing to the new domain, this way I could set up the new Wordpress blog privately (no-one else would know where it was) under the correct domain name, whilst still having access to the old blog to copy content over from.

My first issue was how to transfer the posts from the old blog to the new. I acheived this by doing a few minor hacks to serendipity, and using the export function (where you can export all posts as an RSS feed). To do this, I had to disable the “extended body” feature (i.e. make sure it was output as part of the feed), as explained in solution, part 1, here. Make sure your browser is not caching at this stage!

Once I had acquired the RSS file, I then had to convert it into a format that wordpress would understand. I cheated and wrote a very bad PHP file, here:

Convert s9y RSS into Wordpress-compatible format (convert_s9y_rss.phps) download
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
//Import the feed
$rss = file_get_contents('s9y.rss');
//Opening <![CDATA[s
$rss = str_replace("<content:encoded>","<content:encoded><![CDATA[",$rss);
//Closing  ]]>s
$rss = str_replace("</content:encoded>","]]></content:encoded>",$rss);
//Now replace all newline characters with a " " (this will BREAK any preformatted tags, but will stop wordpress putting <br />s everywhere
$rss = str_replace(array("\n","\r")," ",$rss);
//Finally remove all the htmlentities from the file and output to STDOUT, which you can then redirect to a file
echo html_entity_decode($rss,ENT_COMPAT,'UTF-8');

// I called this as convert_s9y_rss.php > wordpress.rss

I then used Wordpress’ RSS importer to import the posts (no comments, unfortunately). I then copied all of the uploaded files into the same file structure on the new site. The next thing to do was to go back through and edit all the posts and update their links. Only joking, I really couldn’t be bothered to do that! Instead, I made a folder called “serendipity” in the webroot (all of my posts were /serendipity/archives/… previously), and placed in it the following two files:

(s9y_htaccess) download
1
2
3
RewriteEngine On
#Direct *EVERYTHING* to the index.php file
RewriteRule .* index.php [L]
(s9y_index.phps) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
//What URI was I accessed as?
$uri = $_SERVER['REQUEST_URI'];

//Remove everything except the last section
$uri = explode("/",$uri);
$uri = array_pop($uri);

//Convert to lower case (as in Wordpress)
$uri = strtolower($uri);

//Remove the post id from the beginning of the post
$uri = explode("-",$uri);
array_shift($uri);
$uri = implode("-",$uri);

//Remove the extension (.html)
$uri = explode(".",$uri);
array_pop($uri);
$uri = implode(".",$uri);


// Now send a 301 Moved Permanently and the new location
header("Location: /$uri",TRUE,301);
exit();

These caused all posts links to be re-written to a guess at the page name, and thankfully Wordpress was clever enough to work out what was meant. I am not sure if it worked for all posts, but it did for all that I tested. I hope this helps someone, if so leave me a comment (please! I lost all my old comments in the move!).

Comments