In NodeJS + Express, how to automatically remove URL 'www'? for example, client connect to https://www.stackoverflow.com then, https://stackoverflow.com connected! how to automatically remove URL 'www'?
Asked
Active
Viewed 284 times
1 Answers
0
You may try below middleware right somewhere in beginning.
var wwwRedirect = function(req, res, next){
if(req.get('host').indexOf('www.') === 0){
if(req.method === "GET" && !req.xhr){
return res.redirect(req.protocol + '://' + req.get('host').substring(4) + req.originalUrl);
}
}
}
next();
};
app.use(wwwRedirect);
ow3n
- 5,268
- 4
- 47
- 51
codeofnode
- 17,161
- 25
- 81
- 134