Skip to content

Instantly share code, notes, and snippets.

@kkeeth
Last active August 6, 2020 03:14
Show Gist options
  • Save kkeeth/b18841b937b6f80972ba6a0c96c9a6fc to your computer and use it in GitHub Desktop.
Save kkeeth/b18841b937b6f80972ba6a0c96c9a6fc to your computer and use it in GitHub Desktop.
Lambda@Edge の index.html のリダイレクト
'use strict';
// ref. https://qiita.com/GussieTech/items/3ae77dbcb1e79222a9bc
const SUFFIX = '/index.html';
const REGEX_SUFFIX_LESS = /\/[^/.]+$/;
const APPEND_TO_DIRS = 'index.html';
const REGEX_TRAILING_SLASH = /.+\/$/;
exports.handler = (event, context, callback) => {
let request = event.Records[0].cf.request;
const requestUri = request.uri
if (requestUri.match(REGEX_SUFFIX_LESS)) {
request.uri = requestUri + SUFFIX;
// console.log(`requestUri: ${requestUri}`, `rewroteUri: ${request.uri}`)
return callback(null, request);
}
if (requestUri.match(REGEX_TRAILING_SLASH)) {
request.uri = requestUri + APPEND_TO_DIRS;
// console.log(`requestUri: ${requestUri}`, `rewroteUri: ${request.uri}`)
return callback(null, request);
}
// console.log(`requestUri: ${requestUri}`, `rewroteUri: ${request.uri}`)
return callback(null, request);
};
{
"Records": [
{
"cf": {
"config": {
"distributionId": "EXAMPLE"
},
"request": {
"uri": "/hello/index.html",
"method": "GET",
"clientIp": "2001:cdba::3257:9652",
"headers": {
"host": [
{
"key": "Host",
"value": "d123.cf.net"
}
],
"user-agent": [
{
"key": "User-Agent",
"value": "Test Agent"
}
],
"user-name": [
{
"key": "User-Name",
"value": "aws-cloudfront"
}
]
}
}
}
}
]
}
{
"Records": [
{
"cf": {
"config": {
"distributionId": "EXAMPLE"
},
"request": {
"uri": "/hello",
"method": "GET",
"clientIp": "2001:cdba::3257:9652",
"headers": {
"host": [
{
"key": "Host",
"value": "d123.cf.net"
}
],
"user-agent": [
{
"key": "User-Agent",
"value": "Test Agent"
}
],
"user-name": [
{
"key": "User-Name",
"value": "aws-cloudfront"
}
]
}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment