auto redirect www/no-www
This commit is contained in:
parent
cda9bfa418
commit
8c5e5435fc
|
@ -1,3 +1,6 @@
|
||||||
|
# sudo systemctl daemon-reload
|
||||||
|
# sudo systemctl restart greenlock-express
|
||||||
|
# sudo journalctl -xefu greenlock-express
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Greenlock Static Server
|
Description=Greenlock Static Server
|
||||||
Documentation=https://git.coolaj86.com/coolaj86/greenlock-express.js/
|
Documentation=https://git.coolaj86.com/coolaj86/greenlock-express.js/
|
||||||
|
|
|
@ -78,6 +78,7 @@ function checkWwws(_hostname) {
|
||||||
hostname = hostname.slice(4);
|
hostname = hostname.slice(4);
|
||||||
hostdir = path.join(srv, hostname);
|
hostdir = path.join(srv, hostname);
|
||||||
return fs.readdir(hostdir).then(function () {
|
return fs.readdir(hostdir).then(function () {
|
||||||
|
// TODO list both domains?
|
||||||
return hostname;
|
return hostname;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,6 +86,7 @@ function checkWwws(_hostname) {
|
||||||
hostname = 'www.' + hostname;
|
hostname = 'www.' + hostname;
|
||||||
hostdir = path.join(srv, hostname);
|
hostdir = path.join(srv, hostname);
|
||||||
return fs.readdir(hostdir).then(function () {
|
return fs.readdir(hostdir).then(function () {
|
||||||
|
// TODO list both domains?
|
||||||
return hostname;
|
return hostname;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -101,6 +103,13 @@ function myVhostApp(req, res) {
|
||||||
// We could cache wether or not a host exists for some amount of time
|
// We could cache wether or not a host exists for some amount of time
|
||||||
var fin = finalhandler(req, res);
|
var fin = finalhandler(req, res);
|
||||||
return checkWwws(req.headers.host).then(function (hostname) {
|
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 });
|
var serve = serveStatic(path.join(srv, hostname), { redirect: true });
|
||||||
serve(req, res, fin);
|
serve(req, res, fin);
|
||||||
}).catch(function () {
|
}).catch(function () {
|
||||||
|
|
Loading…
Reference in New Issue