From 8c5e5435fc263374c9f464b187d56e2d76d11d00 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 30 Mar 2019 02:35:23 -0600 Subject: [PATCH] auto redirect www/no-www --- dist/etc/systemd/system/greenlock-express.service | 3 +++ examples/vhost.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/dist/etc/systemd/system/greenlock-express.service b/dist/etc/systemd/system/greenlock-express.service index fcdad12..7cd34d2 100644 --- a/dist/etc/systemd/system/greenlock-express.service +++ b/dist/etc/systemd/system/greenlock-express.service @@ -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/ diff --git a/examples/vhost.js b/examples/vhost.js index a325b6f..86bad0f 100644 --- a/examples/vhost.js +++ b/examples/vhost.js @@ -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(""); + return; + } var serve = serveStatic(path.join(srv, hostname), { redirect: true }); serve(req, res, fin); }).catch(function () {