Created
November 2, 2021 14:01
-
-
Save nariyu/fe72a65736f843c34b11e4818eec2ff3 to your computer and use it in GitHub Desktop.
ディレクトリアクセスに index.html を補完する Lambda@Edge 関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
exports.handler = (event, context, callback) => { | |
// リクエスト | |
const request = event.Records[0].cf.request; | |
// `/moja` から `/moja/` にリダイレクト | |
if (request.uri.match(/\/([^\.\/]+)$/)) { | |
const url = request.uri.replace(/(\/[^\.\/]+\/?)$/, '$1/').replace(/\/\/+/g, '/'); | |
const response = { | |
status: '302', | |
statusDescription: 'Found', | |
body: 'Location: ' + url, | |
headers: { | |
'location': [{key: 'Location', value: url}] | |
}, | |
}; | |
return callback(null, response); | |
} | |
// スラッシュで終わっていたら index.html を補完 | |
request.uri = request.uri.replace(/(\/[^\.\/]+\/?)$/, '$1/index.html').replace(/\/\/+/g, '/'); | |
// リクエストを続ける | |
callback(null, request); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment