Hi, my name is Christoffer and I am an Internet addict.

How to capture and redirect old blog post URLs to a stand alone Ghost blog instance

2015-12-19 19:54

Let's say you are moving your whole old blog to your brand new Ghost stand alone instance.

Then you most likely probably have some old blog post URLs you would like to make sure still works, even though you are moving to Ghost.

For example your old post URL:

should now go to:

For me personally, I had around 50 legacy URLs I wanted to point to new Ghost blog posts.

You can of course solve this in various ways, such as creating (reverse) proxies and URL rewrites using various softwares such as Apache webserver or nginx. However since I am hosting Ghost as a one-click DigitalOcean app, I wasn't interested in maintaining a second service just for routing the legacy blog post URLs.

Luckily, if you feel comfortable with JavaScript, Node.js and Regular Expressions, then you can create these redirects directly in the Ghost instance.

What you need to do is open up the file /ghost/core/server/routes/frontend.js and scroll down to where the first routing is declared:

frontendRoutes = function frontendRoutes(middleware) {
    var router = express.Router(),
        subdir = config.paths.subdir,
        routeKeywords = config.routeKeywords,
        indexRouter = express.Router(),
        tagRouter = express.Router({mergeParams: true}),
        authorRouter = express.Router({mergeParams: true}),
        rssRouter = express.Router({mergeParams: true}),
        privateRouter = express.Router();


    // ### Admin routes
    router.get(/^\/(logout|signout)\/$/, function redirectToSignout(req, res) {
        /*jslint unparam:true*/
        res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S});
        res.redirect(301, subdir + '/ghost/signout/');

Then you can include your own routing that does 301 permanent redirects, by including code similar to this:

router.get(/^\/post\/.+$/, function redirectOldURLRouter(req, res) {

	var redirects = {
		"/post/2011-11-26-mypost/": "/2011-11-26-mypost/",
		"/post/2011-11-26-mypost.html/": "/2011-11-26-mypost/"

	var redirectPath = redirects[req.url];
	var redirectURL;

	if ( redirectPath ) {

		redirectURL = "" + redirectPath;
		console.log("REDIRECT", req.url + " -> " + redirectURL);

	} else {

		redirectURL = "" + req.url;
		console.log("REDIRECT, BUT NO HIT", req.url + " -> " + redirectURL)


	res.redirect(301, redirectURL);


Restart Ghost and try out an old URL. It should be redirected correctly to your new blog post!

Leave blog comments and discuss on Reddit