Fix redirection to url without trailing slashes not considering about config urlpath

This commit is contained in:
Wu Cheng-Han 2016-12-12 10:50:43 +08:00
parent 46752194e4
commit 38505491ae
1 changed files with 5 additions and 2 deletions

7
app.js
View File

@ -182,11 +182,14 @@ app.use(function(req, res, next) {
next(); next();
}); });
// redirect url with trailing slashes // redirect url without trailing slashes
app.use(function(req, res, next) { app.use(function(req, res, next) {
if ("GET" == req.method && req.path.substr(-1) == '/' && req.path.length > 1) { if ("GET" == req.method && req.path.substr(-1) == '/' && req.path.length > 1) {
var query = req.url.slice(req.path.length); var query = req.url.slice(req.path.length);
res.redirect(301, config.serverurl + req.path.slice(0, -1) + query); var urlpath = req.path.slice(0, -1);
var serverurl = config.serverurl;
if (config.urlpath) serverurl = serverurl.slice(0, -(config.urlpath.length + 1));
res.redirect(301, serverurl + urlpath + query);
} else { } else {
next(); next();
} }