If you have been following me on twitter, you might already know that I made a move to Ghost. I must say that so far I have been very happy with it. Its fast, its clean and its fun. On the top of it, shifting everything to ghost wasn’t very difficult. There is this wordpress plugin that exports everything in the format the ghost expects, and then all you need to do is to import it on your ghost blog. It doesn’t export comments or images, so make sure that you have the comments synchronized with Disqus or something similar before exporting and images on Cloudinary.

If you are looking to self host your ghost blog, there’s a big community out there trying out ghost and there is no shortage of resources. Here’s one that I found particularly useful when trying to deploy my blog on S3.

Of course, Ghost is a completely different CMS and not everything might work out of the box when making the switch. One such problem that I faced was with the change in url format between wordpress and ghost. Ghost doesn’t have categories and uses a /slug to identify posts. My wordpress blog was set to use /category/slug.

I didn’t want to lose links to my blog because of the change in url, so I decided to dig a bit in the ghost code to check out their routing structure. The core files for ghost are inside /core directory in the ghost installation. The file that is interesting for us is /core/server/routes/frontend.js that handles the routes for our blog frontend. In order to identify url of the form of /category/slug add the following after the route that handles editing of posts (if you add it before the edit route, the edit url will never be captured as it will take a higher priotity).

// ...
server.get(/^\/([0-9]{4}\/[0-9]{2}\/[0-9]{2}\/)?([^\/.]*)\/edit\/$/, frontend.edit);

// Route to handle posts from wordpress in the format category/slug. Redirect to /slug
//  Given `/category/slug/` params will be [undefined, category, undefined, slug]
server.get(/^\/([0-9]{4}\/[0-9]{2}\/[0-9]{2}\/)?([^\/.]*)\/([0-9]{4}\/[0-9]{2}\/[0-9]{2}\/)?([^\/.]*)\/$/, redirectFromWp);

server.get('/', frontend.homepage);

function redirectFromWp(req, res, next) {
	return res.redirect(req.params[3]);
}
// ...

For programmers, all I am doing here is capturing all urls that have a category/slug format and redirecting then to /slug. This can be further made better with checks to ensure that the slug exists etc., but I like to keep it simple.

Update: If you are using ghost 0.5, you need to do the following:

function redirectFromWp(req, res, next) {  
    return res.redirect(301, '/' + req.params[3]);
}

You will need to restart your server to make sure that the changes are reflected.