如何使用Express JS路由器在Node JS项目中进行301重定向?

时间:2015-03-18 12:47:16

标签: javascript node.js express

我有Topic Search和Google以及所有搜索引擎都想拥有301重定向,我的项目是在Heroku Cloud使用节点服务器上设置的,并且有问题,将301重定向'domain.com'到'www .domain.com'....

1 个答案:

答案 0 :(得分:0)

所以我发现问题并修复了,在我的情况下 request.headers.host 的所有内容都只是 domain.com ,功能是:

app.get('/', function(request, response, next) {
if(request.headers.host =="domain.com") {
    response.writeHead(301, {'Location':'http://www.domain.com'+ request.url, 'Expires': (new Date).toGMTString()});
    response.end();
}
else{
    next();
}})