Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Created February 1, 2019 10:25
Show Gist options
  • Save olegpolyakov/2f25680e9e8ba7069dff40c8cb98b06b to your computer and use it in GitHub Desktop.
Save olegpolyakov/2f25680e9e8ba7069dff40c8cb98b06b to your computer and use it in GitHub Desktop.
Express middleware to ensure that there is no www in the url.
function noWww(req, res, next) {
if (req.hostname.match(/^www\./) !== null) {
res.redirect(301, req.protocol + '://' + req.hostname.replace(/^www\./, '') + req.originalUrl);
} else {
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment