Created
June 18, 2014 21:43
-
-
Save pajtai/8745e572c8de0afaf8c8 to your computer and use it in GitHub Desktop.
Express 4 redirect www.domain.com to domain.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this as the first middleware.