Skip to content

Instantly share code, notes, and snippets.

@pajtai
Created June 18, 2014 21:43
Show Gist options
  • Select an option

  • Save pajtai/8745e572c8de0afaf8c8 to your computer and use it in GitHub Desktop.

Select an option

Save pajtai/8745e572c8de0afaf8c8 to your computer and use it in GitHub Desktop.
Express 4 redirect www.domain.com to domain.com
express.use(function(req,res,next){
var host = req.get('host');
if(/^www\./.test(host)){
host = host.substring(4, host.length);
res.writeHead(301, {'Location':req.protocol + '://' + host + req.originalUrl,
'Expires': new Date().toGMTString()});
res.end();
} else {
next();
}
});
@pajtai
Copy link
Copy Markdown
Author

pajtai commented Jun 18, 2014

Use this as the first middleware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment