auto redirect www/no-www

This commit is contained in:
AJ ONeal 2019-03-30 02:35:23 -06:00
parent cda9bfa418
commit 8c5e5435fc
2 changed files with 12 additions and 0 deletions

View File

@ -1,3 +1,6 @@
# sudo systemctl daemon-reload
# sudo systemctl restart greenlock-express
# sudo journalctl -xefu greenlock-express
[Unit]
Description=Greenlock Static Server
Documentation=https://git.coolaj86.com/coolaj86/greenlock-express.js/

View File

@ -78,6 +78,7 @@ function checkWwws(_hostname) {
hostname = hostname.slice(4);
hostdir = path.join(srv, hostname);
return fs.readdir(hostdir).then(function () {
// TODO list both domains?
return hostname;
});
} else {
@ -85,6 +86,7 @@ function checkWwws(_hostname) {
hostname = 'www.' + hostname;
hostdir = path.join(srv, hostname);
return fs.readdir(hostdir).then(function () {
// TODO list both domains?
return hostname;
});
}
@ -101,6 +103,13 @@ function myVhostApp(req, res) {
// We could cache wether or not a host exists for some amount of time
var fin = finalhandler(req, res);
return checkWwws(req.headers.host).then(function (hostname) {
if (hostname !== req.headers.host) {
res.statusCode = 302;
res.setHeader('Location', 'https://' + hostname);
// SECURITY this is safe only because greenlock disallows invalid hostnames
res.end("<!-- redirecting to https://" + hostname + "-->");
return;
}
var serve = serveStatic(path.join(srv, hostname), { redirect: true });
serve(req, res, fin);
}).catch(function () {