diff --git a/lib/index.js b/lib/index.js index 10b173f..164f1a0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -27,6 +27,10 @@ module.exports = (server, pluginOptions) => { // support both '{ /path: /redirect}' and { redirects : { /path: /redirect} } additionalRoutes = additionalRoutes.redirects || additionalRoutes; Object.keys(additionalRoutes).forEach(path => { + if (!path.startsWith('/')) { + additionalRoutes[`/${path}`] = additionalRoutes[path]; + path = `/${path}`; + } server.route({ method: '*', path, diff --git a/test/redirects.test.js b/test/redirects.test.js index b410c5d..f6d4b8b 100644 --- a/test/redirects.test.js +++ b/test/redirects.test.js @@ -150,6 +150,24 @@ lab.experiment('hapi-redirect', () => { Code.expect(result.headers.location).to.equal('/it/works?query=1'); }); + lab.test(' test -> /it/works', async() => { + await server.register({ + plugin: redirectModule, + options: { + redirects: { + test: '/it/works', + }, + } + }); + await server.start(); + const result = await server.inject({ + method: 'get', + url: '/test' + }); + Code.expect(result.statusCode).to.equal(301); + Code.expect(result.headers.location).to.equal('/it/works'); + }); + lab.test(' /from/{param}/?query=1 -> /to/{param}?query=1', async() => { await server.register({ plugin: redirectModule,