From 7e211a307c92b48e4a8b0c9b6ce17620211ca018 Mon Sep 17 00:00:00 2001 From: Katharina Irrgang Date: Mon, 11 Sep 2017 14:20:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20Fixed=20custom=20redirects=20?= =?UTF-8?q?with=20query/search=20params=20(#8998)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #8997 - improved the logic for custom redirects - added more tests --- core/server/middleware/custom-redirects.js | 9 +++++++-- core/test/functional/routes/frontend_spec.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/server/middleware/custom-redirects.js b/core/server/middleware/custom-redirects.js index 0978ea505f..8287a48535 100644 --- a/core/server/middleware/custom-redirects.js +++ b/core/server/middleware/custom-redirects.js @@ -1,5 +1,6 @@ var fs = require('fs-extra'), _ = require('lodash'), + url = require('url'), debug = require('ghost-ignition').debug('custom-redirects'), config = require('../config'), errors = require('../errors'), @@ -43,13 +44,17 @@ module.exports = function redirects(blogApp) { } blogApp.get(new RegExp(redirect.from), function (req, res) { - var maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0; + var maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0, + parsedUrl = url.parse(req.originalUrl); res.set({ 'Cache-Control': 'public, max-age=' + maxAge }); - res.redirect(redirect.permanent ? 301 : 302, req.originalUrl.replace(new RegExp(redirect.from), redirect.to)); + res.redirect(redirect.permanent ? 301 : 302, url.format({ + pathname: parsedUrl.pathname.replace(new RegExp(redirect.from), redirect.to), + search: parsedUrl.search + })); }); }); } catch (err) { diff --git a/core/test/functional/routes/frontend_spec.js b/core/test/functional/routes/frontend_spec.js index c0e022bc8c..2a21b9dfb3 100644 --- a/core/test/functional/routes/frontend_spec.js +++ b/core/test/functional/routes/frontend_spec.js @@ -786,6 +786,26 @@ describe('Frontend Routing', function () { }); }); + it('with query params', function (done) { + request.get('/topic?something=good') + .expect(302) + .expect('Cache-Control', testUtils.cacheRules.public) + .end(function (err, res) { + res.headers.location.should.eql('/?something=good'); + doEnd(done)(err, res); + }); + }); + + it('with query params', function (done) { + request.get('/post/10/a-nice-blog-post?a=b') + .expect(302) + .expect('Cache-Control', testUtils.cacheRules.public) + .end(function (err, res) { + res.headers.location.should.eql('/a-nice-blog-post?a=b'); + doEnd(done)(err, res); + }); + }); + it('should not redirect', function (done) { request.get('/post/a-nice-blog-post/') .end(function (err, res) {