简体   繁体   English

Node.js:301重定向非www而不表达

[英]Node.js: 301 redirect non-www without express

For an existing project, I'd like to just do a simple change of redirecting www.mysite.com to mysite.com (because cookie issues, cookies on www. isn't accessible to non-www version). 对于现有项目,我只想简单地将www.mysite.com重定向到mysite.com(因为cookie问题,www。上的cookie无法访问非www版本)。 I do not want to include express. 我不想包括快递。

How can I do this simple change? 我该怎么做这个简单的改变?

I think this is what you're looking for: 我想这就是你要找的东西:

var http = require("http");

    http.createServer(function (req, res) {
    res.writeHead(301, {"Location": "http://example.com"});
    res.end();

}).listen(80);

UPDATE : Its not the suitable answer for question. 更新:它不是问题的合适答案。

Why dont you try to filter non www request with this 为什么不尝试使用此过滤非www请求

app.get ('/*', function (req, res, next){
if (req.headers.host.match(/^www\./))

  {
    res.writeHead (301, {'Location': 'http://example.com'});

    }
else { 

   return next();
    }

} );

You should consider it only for express and if you want to like redirect before express then you should try Nginx before express or any reverse proxy server so that request can be filter before sending to express. 您应该只考虑快递,如果您想在快递之前重定向,那么您应该在快递之前尝试Nginx或任何反向代理服务器,以便在发送到快递之前可以过滤请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM