🐛 Fixed custom redirects with query/search params (#8998)

closes #8997

- improved the logic for custom redirects
- added more tests
This commit is contained in:
Katharina Irrgang 2017-09-11 14:20:29 +02:00 committed by Hannah Wolfe
parent 45fd2d437f
commit 7e211a307c
2 changed files with 27 additions and 2 deletions

View File

@ -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) {

View File

@ -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) {