Redirecting www domain to non www on Ghost

Sure. There are other ways you can do this. .htaccess on apache, or a config in nginx. But one of the cool things about node.js is that It can be its own webserver. And in many cases, thats exactly how your ghost blog could be running. That’s how mine runs.

Since Ghost is built using express.js the redirect rules can be right in your codebase.

To redirect www to non-www in ghost, you simply need to open up your frontend.js file which is located in

ghost/core/server/routes/frontend.js

And add this block of code before all of the other routes.

server.get('/*', function(req, res, next) {
  if (req.headers.host.match(/^www/) !== null ) {
    res.redirect(301, 'http://' + req.headers.host.replace(/^www\./, '') + req.url);
  } else {
    next();
  }
});

This will catch the request, and if the www is included in the url, does a 301 redirect to the non-www url.

Restart Ghost and you are done.