Skip to content

Commit

Permalink
🐛 Fixed custom redirects with query/search params (#8998)
Browse files Browse the repository at this point in the history
closes #8997

- improved the logic for custom redirects
- added more tests
  • Loading branch information
kirrg001 authored and ErisDS committed Sep 11, 2017
1 parent 45fd2d4 commit 7e211a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/server/middleware/custom-redirects.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions core/test/functional/routes/frontend_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7e211a3

Please sign in to comment.