Beginning NodeJS for PHP Devs: Getting Started Locally

You’re no stranger to locally hosted php. Especially using MAMP or XMPP. Here is how you get started locally with NodeJS.

Getting started setting up your first node.js app up locally on OS X is so easy it makes my eyes tingle. I have always developed on a Mac, so if you are interested in windows, it’s probably best to find someone who specializes in that. If this doesn’t concern you, read on!

Continue reading “Beginning NodeJS for PHP Devs: Getting Started Locally”

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.