forked from svrjs/svrjs
Added trailing slash redirect support
This commit is contained in:
parent
1f603ec262
commit
5321f2c6a7
2 changed files with 38 additions and 4 deletions
34
svr.js
34
svr.js
|
@ -4293,6 +4293,35 @@ if (!cluster.isPrimary) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trailing slash redirection
|
||||||
|
function redirectTrailingSlashes(callback) {
|
||||||
|
if (!configJSON.disableTrailingSlashRedirects && href[href.length-1] != "/") {
|
||||||
|
fs.stat("." + decodeURIComponent(href), function (err, stats) {
|
||||||
|
if(err || !stats.isDirectory()) {
|
||||||
|
try {
|
||||||
|
callback();
|
||||||
|
} catch(ex) {
|
||||||
|
callServerError(500, undefined, ex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var destinationURL = uobject;
|
||||||
|
destinationURL.path = null;
|
||||||
|
destinationURL.href = null;
|
||||||
|
destinationURL.pathname += "/";
|
||||||
|
destinationURL.hostname = null;
|
||||||
|
destinationURL.host = null;
|
||||||
|
destinationURL.port = null;
|
||||||
|
destinationURL.protocol = null;
|
||||||
|
destinationURL.slashes = null;
|
||||||
|
destinationURL = url.format(destinationURL);
|
||||||
|
redirect(destinationURL);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle HTTP authentication
|
// Handle HTTP authentication
|
||||||
if (authIndex > -1) {
|
if (authIndex > -1) {
|
||||||
var authcode = nonStandardCodes[authIndex];
|
var authcode = nonStandardCodes[authIndex];
|
||||||
|
@ -4423,7 +4452,9 @@ if (!cluster.isPrimary) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serverconsole.reqmessage("Client is logged in as \"" + username + "\"");
|
serverconsole.reqmessage("Client is logged in as \"" + username + "\"");
|
||||||
|
redirectTrailingSlashes(function () {
|
||||||
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData));
|
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
callServerError(500, undefined, generateErrorStack(err));
|
callServerError(500, undefined, generateErrorStack(err));
|
||||||
|
@ -4470,7 +4501,9 @@ if (!cluster.isPrimary) {
|
||||||
process.send("\x12AUTHQ" + reqip);
|
process.send("\x12AUTHQ" + reqip);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
redirectTrailingSlashes(function () {
|
||||||
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData));
|
modExecute(mods, vres(req, res, serverconsole, responseEnd, href, ext, uobject, search, "index.html", users, page404, head, foot, "", callServerError, getCustomHeaders, origHref, redirect, parsePostData));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -5485,6 +5518,7 @@ function saveConfig() {
|
||||||
if (configJSONobj.errorPages === undefined) configJSONobj.errorPages = [];
|
if (configJSONobj.errorPages === undefined) configJSONobj.errorPages = [];
|
||||||
if (configJSONobj.useWebRootServerSideScript === undefined) configJSONobj.useWebRootServerSideScript = true;
|
if (configJSONobj.useWebRootServerSideScript === undefined) configJSONobj.useWebRootServerSideScript = true;
|
||||||
if (configJSONobj.exposeModsInErrorPages === undefined) configJSONobj.exposeModsInErrorPages = true;
|
if (configJSONobj.exposeModsInErrorPages === undefined) configJSONobj.exposeModsInErrorPages = true;
|
||||||
|
if (configJSONobj.disableTrailingSlashRedirects === undefined) configJSONobj.disableTrailingSlashRedirects = false;
|
||||||
|
|
||||||
var configString = JSON.stringify(configJSONobj, null, 2);
|
var configString = JSON.stringify(configJSONobj, null, 2);
|
||||||
fs.writeFileSync(__dirname + "/config.json", configString);
|
fs.writeFileSync(__dirname + "/config.json", configString);
|
||||||
|
|
Reference in a new issue