mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-03 08:25:06 +03:00
🐛 Fixed custom redirects with query/search params (#8998)
closes #8997 - improved the logic for custom redirects - added more tests
This commit is contained in:
parent
45fd2d437f
commit
7e211a307c
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user