-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refs #7707 - be able to add a custom redirect file into the content folder - define redirects as JSON format The redirects feature is already present in the LTS branch. I was not able to cherry-pick over, too many changes or conflicts. Creating a PR to ensure 1. tests pass and 2. overview of code changes. I had to add an example active theme to our test fixture utils, because otherwise Ghost will complain when forking Ghost.
- Loading branch information
Showing
18 changed files
with
696 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ urlRedirects = function urlRedirects(req, res, next) { | |
})); | ||
} | ||
|
||
debug('no url redirect'); | ||
next(); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Oops, something went wrong.