diff --git a/core/server/services/routing/controllers/entry.js b/core/server/services/routing/controllers/entry.js index 7fcd416d4f..67faf9124e 100644 --- a/core/server/services/routing/controllers/entry.js +++ b/core/server/services/routing/controllers/entry.js @@ -1,4 +1,5 @@ const debug = require('ghost-ignition').debug('services:routing:controllers:entry'), + url = require('url'), urlService = require('../../url'), filters = require('../../../filters'), helpers = require('../helpers'); @@ -63,10 +64,18 @@ module.exports = function entryController(req, res, next) { * * The resource url always contains the subdirectory. This was different before dynamic routing. * That's why we have to use the original url, which contains the sub-directory. + * + * @NOTE + * + * post.url contains the subdirectory if configured. */ - if (post.url !== req.originalUrl) { + if (post.url !== url.parse(req.originalUrl).pathname) { debug('redirect'); - return urlService.utils.redirect301(res, post.url); + + return urlService.utils.redirect301(res, url.format({ + pathname: post.url, + search: url.parse(req.originalUrl).search + })); } helpers.secure(req, post); diff --git a/core/test/unit/services/routing/controllers/entry_spec.js b/core/test/unit/services/routing/controllers/entry_spec.js index 318bce9283..ea97e39396 100644 --- a/core/test/unit/services/routing/controllers/entry_spec.js +++ b/core/test/unit/services/routing/controllers/entry_spec.js @@ -177,5 +177,33 @@ describe('Unit - services/routing/controllers/entry', function () { controllers.entry(req, res); }); + + it('requested url !== resource url: with query params', function (done) { + post.url = '/2017/08/' + post.url; + req.path = '/2017/07/' + post.url; + req.originalUrl = req.path + '?query=true'; + + res.locals.routerOptions.type = 'posts'; + + urlService.getResource.withArgs(post.url).returns({ + config: { + type: 'posts' + } + }); + + postLookUpStub.withArgs(req.path, res.locals.routerOptions) + .resolves({ + post: post + }); + + urlService.utils.redirect301.callsFake(function (res, postUrl) { + postUrl.should.eql(post.url + '?query=true'); + done(); + }); + + controllers.entry(req, res, function (err) { + done(err); + }); + }); }); });