简体   繁体   中英

How to make 301 redirect in Node JS project with Express JS router?

我有主题搜索和Google,并且所有搜索引擎都希望具有301重定向,我的项目是在Heroku Cloud使用节点服务器上设置的,并且在将301“ domain.com”重定向到“ www.domain.com”时遇到了问题。

So I found the problem and fixed, all is coming of request.headers.host in my case is only domain.com and the function is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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