Last active
August 6, 2020 03:14
-
-
Save kkeeth/b18841b937b6f80972ba6a0c96c9a6fc to your computer and use it in GitHub Desktop.
Lambda@Edge の index.html のリダイレクト
This file contains hidden or 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'; | |
// 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); | |
}; |
This file contains hidden or 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
{ | |
"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" | |
} | |
] | |
} | |
} | |
} | |
} | |
] | |
} |
This file contains hidden or 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
{ | |
"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