Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/middleware/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var _ = require('lodash');

var minimatch = require('minimatch');
var pathToRegexp = require('path-to-regexp');
var slasher = require('glob-slasher');

function formatExternalUrl(u) {
var cleaned = u
Expand All @@ -32,10 +31,22 @@ function addQuery(url, qs) {

var Redirect = function(source, destination, type) {
this.type = type || 301;
this.source = slasher(source);

// no leading slash
if (source[0] !== '/') {
// glob pattern but no leading slash
if (source[0] === '!' && source[1] && source[1] !== '/') {
this.source = '!/' + source.substring(2);
} else {
this.source = '/' + source;
}
} else {
this.source = source;
}

this.destination = destination;

if (this.destination.match(/(?:^|\/):/)) {
if (this.source.match(/(?:^|\/):/)) {
this.captureKeys = [];
this.capture = pathToRegexp(this.source, this.captureKeys);
this.compileDestination = pathToRegexp.compile(this.destination);
Expand All @@ -51,9 +62,11 @@ Redirect.prototype.test = function(url) {
}

var match;

if (this.capture) {
match = this.capture.exec(url);
}

if (match) {
var params = {};
for (var i = 0; i < this.captureKeys.length; i++) {
Expand Down