Skip to content

Commit c4716e8

Browse files
author
deiu
committed
Redirect /foo to /foo/ even when handling index.html
1 parent 2c7a5d9 commit c4716e8

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

lib/handlers/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ function handler (req, res, next) {
1414
var requestedType = negotiator.mediaType()
1515
var filename = utils.reqToPath(req)
1616

17-
if (requestedType.indexOf('text/html') !== 0) {
18-
return next()
19-
}
20-
2117
ldp.stat(filename, function (err, stats) {
2218
if (err) return next()
2319

2420
res.locals.path = req.path
2521
if (stats.isDirectory()) {
22+
// redirect to the right container if missing trailing /
23+
if (req.path.lastIndexOf('/') !== req.path.length - 1) {
24+
return res.redirect(301, path.join(req.path, '/'))
25+
}
26+
27+
if (requestedType.indexOf('text/html') !== 0) {
28+
return next()
29+
}
30+
2631
res.locals.path = path.join(req.path, indexFile)
2732
}
2833
debug('Looking for index in ' + res.locals.path)

test/http.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ describe('HTTP APIs', function () {
278278
})
279279
.end(done)
280280
})
281+
it('should still redirect to the right container URI if missing / and HTML is requested',
282+
function (done) {
283+
server.get('/sampleContainer')
284+
.set('accept', 'text/html')
285+
.expect('location', /\/sampleContainer\//)
286+
.expect(301, done)
287+
})
281288
})
282289

283290
describe('HEAD API', function () {

0 commit comments

Comments
 (0)