diff --git a/app.js b/app.js index 9586c13c..7482e2c5 100644 --- a/app.js +++ b/app.js @@ -34,6 +34,7 @@ apostrophe({ // use vite for asset bundling and hot module reloading '@apostrophecms/vite': {}, // The project's first custom page type. - 'default-page': {} + 'default-page': {}, + 'hostname-redirects': {} } }); diff --git a/modules/hostname-redirects/index.js b/modules/hostname-redirects/index.js new file mode 100644 index 00000000..54f91c16 --- /dev/null +++ b/modules/hostname-redirects/index.js @@ -0,0 +1,22 @@ +export default { + middleware(self) { + return { + redirects(req, res, next) { + if (![ 'GET', 'HEAD' ].includes(req.method)) { + return next(); + } + const protocol = process.env.ENV ? 'https' : 'http'; + const portSuffix = process.env.ENV ? '' : ':3000'; + const mapping = { + 'altfr.localhost': 'fr.localhost', + 'alten.localhost': 'en.localhost' + }; + const mapped = mapping[req.hostname]; + if (mapped) { + return res.redirect(`${protocol}://${mapped}${portSuffix}${req.url}`); + } + return next(); + } + } + } +} \ No newline at end of file