Skip to content

Instantly share code, notes, and snippets.

@runserverjp
Last active July 27, 2022 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runserverjp/039a2e51382597a2ce6fca8bc3ba4b58 to your computer and use it in GitHub Desktop.
Save runserverjp/039a2e51382597a2ce6fca8bc3ba4b58 to your computer and use it in GitHub Desktop.
cloudfront + s3 の環境で、www なしのドメインの場合は www ありのドメイン名にリダイレクトし、/ で終わらない path に /index.html を付ける cloudfront function
// https://www.runserver.jp/posts/2022-05-09-hugo-s3-cloudfront/ も参考
//
// cloudfront ディストリビューションの代替ドメインに www あり、なし両方登録すると、
// route53 で www あり、なしの両方のレコードに、同じディストリビューションを設定することができます。
// (私は、気づかず、小一時間ハマりました)
//
// cloudfront / ビヘイビア / デフォルト / ビューワーリクエスト で
// cloudfront function を設定してこの関数を指定しています。
//
// コピペする時は、runserver.jp の部分は変更して使ってください
// query parameter は付かないので、必要なら付けてください。
function handler(event) {
var request = event.request;
var uri = request.uri;
if (request.headers.host.value == 'runserver.jp') {
var response = {
statusCode: 301,
statusDescription: 'Moved Permanently',
// statusCode: 302,
// statusDescription: 'Found',
headers: {
"location": { "value": "https://www.runserver.jp" + uri }
}
}
return response;
}
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}
return request;
}
@runserverjp
Copy link
Author

Cloudfront directory indexも見てください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment