diff --git a/core/server/blog/app.js b/core/server/blog/app.js
index d83e1792c5..93b95d22fd 100644
--- a/core/server/blog/app.js
+++ b/core/server/blog/app.js
@@ -21,6 +21,7 @@ var debug = require('debug')('ghost:blog'),
serveSharedFile = require('../middleware/serve-shared-file'),
staticTheme = require('../middleware/static-theme'),
themeHandler = require('../middleware/theme-handler'),
+ customRedirects = require('../middleware/redirects'),
serveFavicon = require('../middleware/serve-favicon');
module.exports = function setupBlogApp() {
@@ -39,6 +40,10 @@ module.exports = function setupBlogApp() {
blogApp.use(themeHandler.configHbsForContext);
debug('Themes done');
+ // you can extend Ghost with a custom redirects file
+ // see https://github.com/TryGhost/Ghost/issues/7707
+ customRedirects(blogApp);
+
// Static content/assets
// @TODO make sure all of these have a local 404 error handler
// Favicon
diff --git a/core/server/config/utils.js b/core/server/config/utils.js
index df2410875c..c75036209d 100644
--- a/core/server/config/utils.js
+++ b/core/server/config/utils.js
@@ -75,6 +75,8 @@ exports.getContentPath = function getContentPath(type) {
return path.join(this.get('paths:contentPath'), 'scheduling/');
case 'logs':
return path.join(this.get('paths:contentPath'), 'logs/');
+ case 'data':
+ return path.join(this.get('paths:contentPath'), 'data/');
default:
throw new Error('getContentPath was called with: ' + type);
}
diff --git a/core/server/middleware/redirects.js b/core/server/middleware/redirects.js
new file mode 100644
index 0000000000..06856038fc
--- /dev/null
+++ b/core/server/middleware/redirects.js
@@ -0,0 +1,52 @@
+var fs = require('fs-extra'),
+ _ = require('lodash'),
+ config = require('../config'),
+ errors = require('../errors'),
+ utils = require('../utils');
+
+/**
+ * you can extend Ghost with a custom redirects file
+ * see https://github.com/TryGhost/Ghost/issues/7707
+ * file loads synchronously, because we need to register the routes before anything else
+ */
+module.exports = function redirects(blogApp) {
+ try {
+ var redirects = fs.readFileSync(config.getContentPath('data') + '/redirects.json', 'utf-8');
+ redirects = JSON.parse(redirects);
+
+ _.each(redirects, function (redirect) {
+ if (!redirect.from || !redirect.to) {
+ errors.logError(null, 'Your redirects.json file is in a wrong format');
+ return;
+ }
+
+ /**
+ * always delete trailing slashes, doesn't matter if regex or not
+ * Example:
+ * - you define /my-blog-post-1/ as from property
+ * - /my-blog-post-1 or /my-blog-post-1/ should work
+ */
+ if (redirect.from.match(/\/$/)) {
+ redirect.from = redirect.from.slice(0, -1);
+ }
+
+ if (redirect.from[redirect.from.length - 1] !== '$') {
+ redirect.from += '\/?$';
+ }
+
+ blogApp.get(new RegExp(redirect.from), function (req, res) {
+ var maxAge = redirect.permanent ? utils.ONE_YEAR_S : 0;
+
+ res.set({
+ 'Cache-Control': 'public, max-age=' + maxAge
+ });
+
+ res.redirect(redirect.permanent ? 301 : 302, req.originalUrl.replace(new RegExp(redirect.from), redirect.to));
+ });
+ });
+ } catch (err) {
+ if (err.code !== 'ENOENT') {
+ errors.logAndThrowError(err, 'Your redirects.json is broken.', 'Check if your JSON is valid.');
+ }
+ }
+};
diff --git a/core/server/middleware/url-redirects.js b/core/server/middleware/url-redirects.js
index d4e6952832..89666de1c4 100644
--- a/core/server/middleware/url-redirects.js
+++ b/core/server/middleware/url-redirects.js
@@ -62,6 +62,7 @@ urlRedirects = function urlRedirects(req, res, next) {
}));
}
+ debug('no url redirect');
next();
};
diff --git a/core/server/utils/read-directory.js b/core/server/utils/read-directory.js
index 2a5ad6e7eb..a2bf7370cb 100644
--- a/core/server/utils/read-directory.js
+++ b/core/server/utils/read-directory.js
@@ -82,6 +82,13 @@ function readDirectory(dir, options) {
});
return tree;
+ })
+ .catch(function (err) {
+ if (err.code === 'ENOENT') {
+ return;
+ }
+
+ return Promise.reject(err);
});
}
diff --git a/core/test/functional/routes/frontend_spec.js b/core/test/functional/routes/frontend_spec.js
index d0a53171cc..83d9f589b5 100644
--- a/core/test/functional/routes/frontend_spec.js
+++ b/core/test/functional/routes/frontend_spec.js
@@ -776,5 +776,218 @@ describe('Frontend Routing', function () {
.end(doEnd(done));
});
});
+
+ describe('Redirects (use redirects.json from test/utils/fixtures/data)', function () {
+ var forkedGhost, request;
+
+ before(function (done) {
+ testUtils.fork.ghost({
+ url: 'http://localhost:2370/',
+ server: {
+ port: 2370
+ },
+ paths: {
+ contentPath: 'core/test/utils/fixtures'
+ }
+ }, 'testredirects')
+ .then(function (child) {
+ forkedGhost = child;
+ request = require('supertest');
+ request = request('http://localhost:2370');
+ }).then(done).catch(done);
+ });
+
+ after(function (done) {
+ if (forkedGhost) {
+ forkedGhost.kill(done);
+ } else {
+ done(new Error('No forked ghost process exists, test setup must have failed.'));
+ }
+ });
+
+ describe('1 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/post/10/a-nice-blog-post')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/a-nice-blog-post');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/post/10/a-nice-blog-post/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/a-nice-blog-post');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('should not redirect', function (done) {
+ request.get('/post/a-nice-blog-post/')
+ .end(function (err, res) {
+ res.statusCode.should.not.eql(302);
+ res.statusCode.should.not.eql(301);
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('2 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/my-old-blog-post/')
+ .expect(301)
+ .expect('Cache-Control', testUtils.cacheRules.year)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/revamped-url/');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/my-old-blog-post')
+ .expect(301)
+ .expect('Cache-Control', testUtils.cacheRules.year)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/revamped-url/');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('should not redirect', function (done) {
+ request.get('/my-old-blog-post-1/')
+ .end(function (err, res) {
+ res.statusCode.should.not.eql(302);
+ res.statusCode.should.not.eql(301);
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('3 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/what/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/what-does-god-say');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/what')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/what-does-god-say');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('4 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/search/label/&&&/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/tag/&&&/');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/search/label/&&&/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/tag/&&&/');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('5 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/topic/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/topic')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('6 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/resources/download/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/shubal-stearns');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('without trailing slash', function (done) {
+ request.get('/resources/download')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/shubal-stearns');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('7 case', function () {
+ it('with trailing slash', function (done) {
+ request.get('/2016/11/welcome.html')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/welcome');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+
+ describe('last case', function () {
+ it('default', function (done) {
+ request.get('/prefix/')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/blog/');
+ doEnd(done)(err, res);
+ });
+ });
+
+ it('with a custom path', function (done) {
+ request.get('/prefix/expect-redirect')
+ .expect(302)
+ .expect('Cache-Control', testUtils.cacheRules.public)
+ .end(function (err, res) {
+ res.headers.location.should.eql('/blog/expect-redirect');
+ doEnd(done)(err, res);
+ });
+ });
+ });
+ });
});
});
diff --git a/core/test/utils/fixtures/data/redirects.json b/core/test/utils/fixtures/data/redirects.json
new file mode 100644
index 0000000000..ad471fdf99
--- /dev/null
+++ b/core/test/utils/fixtures/data/redirects.json
@@ -0,0 +1,35 @@
+[
+ {
+ "from": "^/post/[0-9]+/([a-z0-9\\-]+)",
+ "to": "/$1"
+ },
+ {
+ "permanent": true,
+ "from": "/my-old-blog-post/",
+ "to": "/revamped-url/"
+ },
+ {
+ "from": "^\\/what(\\/?)$",
+ "to": "/what-does-god-say"
+ },
+ {
+ "from": "^\\/search\\/label\\/([^\\%20]+)$",
+ "to": "/tag/$1"
+ },
+ {
+ "from": "^\\/topic\\/",
+ "to": "/"
+ },
+ {
+ "from": "^/resources\\/download(\\/?)$",
+ "to": "/shubal-stearns"
+ },
+ {
+ "from": "^\\/[0-9]{4}\\/[0-9]{2}\\/([a-z0-9\\-]+)(\\.html)?(\\/)?$",
+ "to": "/$1"
+ },
+ {
+ "from": "^/prefix/([a-z0-9\\-]+)?",
+ "to": "/blog/$1"
+ }
+]
diff --git a/core/test/utils/fixtures/themes/casper/LICENSE b/core/test/utils/fixtures/themes/casper/LICENSE
new file mode 100644
index 0000000000..97a642728b
--- /dev/null
+++ b/core/test/utils/fixtures/themes/casper/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2013-2016 Ghost Foundation
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
diff --git a/core/test/utils/fixtures/themes/casper/README.md b/core/test/utils/fixtures/themes/casper/README.md
new file mode 100644
index 0000000000..1d787f50dd
--- /dev/null
+++ b/core/test/utils/fixtures/themes/casper/README.md
@@ -0,0 +1,16 @@
+# Casper
+
+The default theme for [Ghost](http://github.com/tryghost/ghost/).
+
+To download, visit the [releases](https://github.com/TryGhost/Casper/releases) page.
+
+## Copyright & License
+
+Copyright (c) 2013-2016 Ghost Foundation - Released under the MIT License.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/core/test/utils/fixtures/themes/casper/author.hbs b/core/test/utils/fixtures/themes/casper/author.hbs
new file mode 100644
index 0000000000..eb7b1de741
--- /dev/null
+++ b/core/test/utils/fixtures/themes/casper/author.hbs
@@ -0,0 +1,41 @@
+{{!< default}}
+{{!-- The tag above means - insert everything in this file into the {body} of the default.hbs template --}}
+
+{{!-- The big featured header --}}
+
+{{!-- Everything inside the #author tags pulls data from the author --}}
+{{#author}}
+
{{excerpt words="26"}} »
+