apostrophe-redirects
Advanced tools
Comparing version 2.2.0 to 2.3.0
78
index.js
@@ -65,2 +65,8 @@ var _ = require('lodash'); | ||
{ | ||
name: 'ignoreQueryString', | ||
label: 'Ignore query string when matching.', | ||
type: 'boolean', | ||
def: false | ||
}, | ||
{ | ||
name: '_newPage', | ||
@@ -104,2 +110,3 @@ type: 'joinByOne', | ||
'urlType', | ||
'ignoreQueryString', | ||
'_newPage', | ||
@@ -140,34 +147,53 @@ 'externalUrl', | ||
self.expressMiddleware = function (req, res, next) { | ||
var slug = req.url; | ||
return self.find(req, { slug: 'redirect-' + slug }, { | ||
title: 1, | ||
slug: 1, | ||
urlType: 1, | ||
pageId: 1, | ||
type: 1, | ||
externalUrl: 1, | ||
redirectSlug: 1, | ||
statusCode: 1, | ||
_newPage: 1 | ||
}).toObject(function (err, result) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
if (result) { | ||
var status = parseInt(result.statusCode); | ||
if (isNaN(status) || !status) { | ||
status = 302; | ||
let slug = req.url; | ||
let pathOnly = self.apos.utils.regExpQuote(slug.split('?')[0]); | ||
let redirectRegEx = new RegExp(`redirect-${pathOnly}.*`); | ||
let redirectResult = self.find(req, { slug: redirectRegEx }, { | ||
title: 1, | ||
slug: 1, | ||
urlType: 1, | ||
ignoreQueryString: 1, | ||
pageId: 1, | ||
type: 1, | ||
externalUrl: 1, | ||
redirectSlug: 1, | ||
statusCode: 1, | ||
_newPage: 1 | ||
}).toArray(function(err, results) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
if (result.urlType === 'internal' && result._newPage) { | ||
return req.res.redirect(status, result._newPage._url); | ||
} else if (result.urlType === 'external' && result.externalUrl.length) { | ||
return req.res.redirect(status, result.externalUrl); | ||
let target; | ||
if (results) { | ||
if (results.some(result => result.redirectSlug == slug)) { | ||
target = results.find(result => result.redirectSlug == slug); | ||
} else if (results.some(result => result.redirectSlug == pathOnly && result.ignoreQueryString)) { | ||
target = results.find(result => result.redirectSlug == pathOnly && result.ignoreQueryString); | ||
} | ||
if (target) { | ||
let status = parseInt(target.statusCode); | ||
if (isNaN(status) || !status) { | ||
status = 302; | ||
} | ||
if (target.urlType === 'internal' && target._newPage) { | ||
return req.res.redirect(status, target._newPage._url); | ||
} else if (target.urlType === 'external' && target.externalUrl.length) { | ||
return req.res.redirect(status, target.externalUrl); | ||
} | ||
} | ||
} | ||
} | ||
return next(); | ||
}); | ||
return next(); | ||
}); | ||
}; | ||
} | ||
}; |
{ | ||
"name": "apostrophe-redirects", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Allows admins to create redirects within an Apostrophe site", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,2 +49,4 @@ This module allows admins to add redirects from one URL to another within an [Apostrophe site](http://apostrophecms.org/). | ||
By default a redirect includes any query string (the `?` and whatever follows it, up to but not including any `#`) on incoming requests when matching for redirection. You can toggle the "ignore query string when matching" option in a redirect definition to ignore query strings on incoming requests and only match on the base URL path. A redirect that does not use this option will always match first, so you can match various specific query strings and then have a fallback rule for other cases. | ||
Be aware that each redirect is live as soon as you save it and that it is possible to make a mess with redirects. In a pinch, you can remove unwanted redirects via the MongoDB command line client (look for `{ type: "apostrophe-redirect" }` in the `aposDocs` collection in MongoDB). | ||
@@ -70,6 +72,8 @@ | ||
2.2.0 Implemented polymorphic joins for internal pages which also makes it possible to configure your own pieces for polymorphic joins. | ||
2.3.0: "Ignore query string" option for times when you'd like to match the URL regardless of the `?` and everything that follows it. Thanks to Shaun Hurley for the contribution. | ||
2.1.0 Disables `apostrophe-site-map` for redirects to prevent more superfluous UI tabs. | ||
2.2.0: Implemented polymorphic joins for internal pages which also makes it possible to configure your own pieces for polymorphic joins. | ||
2.1.0: Disables `apostrophe-site-map` for redirects to prevent more superfluous UI tabs. | ||
2.0.0: Implemented `statusCode` option and user-editable `statusCode` field allowing the user to choose a permanent or temporary redirect. For bc the default is still `302`. If the `statusCode` option is set to `301` instead, permanent redirects will be the default for *new* redirects. Existing redirects may be manually switched to `301` if desired. | ||
@@ -76,0 +80,0 @@ |
11586
179
86