NodeJS Redirect all non-www to www except subdomains

后端 未结 2 637
南旧
南旧 2021-01-21 11:52

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!

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 12:42

    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();
      }
    });
    

提交回复
热议问题