Search code examples
javascriptnode.jswebexpressno-www

NodeJS Redirect all non-www to www except subdomains


any idea on how i can do this in Express 3.0? As the non-www url is causing very odd problems in different areas of the website.

Thanks!


Solution

  • So i found the answer from another question.

    Node.js: 301 redirect non-www without express

    Sorry for not searching before

    app.get ('/*', function (req, res, next){
      if (!req.headers.host.match(/^www\./)){
          res.writeHead (301, {'Location': 'http://mysite.com'});
      }else{ 
         return next();
      }
    });