Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matheusdavidson/70584a8d090ed63590b8b4f28d61127b to your computer and use it in GitHub Desktop.
Save matheusdavidson/70584a8d090ed63590b8b4f28d61127b to your computer and use it in GitHub Desktop.
//... start of the script
server.all(/.*/, function (req, res, next) {
//
// Get host
const host: any = req.header('host');
//
// redirect non www to www
else if (!host.includes('www')) {
res.redirect(301, 'https://www.' + host + req.url);
}
//
// redirect http to https
else if (!req.secure) {
res.redirect(301, 'https://' + host + req.url);
}
//
// it's fine now
else {
next();
}
}
//... end of the script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment