From a7b0752b558253a08072263aff7ace4b6017e11b Mon Sep 17 00:00:00 2001 From: Daisuke Date: Sun, 1 Feb 2026 17:34:11 +0900 Subject: [PATCH] fix(cloudfront): rewrite extensionless paths to index - append /index.html for extensionless requests - document CloudFront Function behavior in README --- README.md | 1 + infra/cfn/template-cloudfront.yaml | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a9222c2..bbf275d 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ It includes CloudFormation templates for provisioning all necessary components, - **Lambda (Go)**: Custom deployment logic executed within the pipeline - **ECR**: Container repository for Lambda packaging - **Secrets Manager**: Secure storage for deployment keys +- **CloudFront Function**: Appends `/index.html` for extensionless paths (e.g. `/blog/2`, `/tags`) All infrastructure is declaratively managed via CloudFormation templates under `infra/cfn/`. diff --git a/infra/cfn/template-cloudfront.yaml b/infra/cfn/template-cloudfront.yaml index 9af45b4..7ae064e 100644 --- a/infra/cfn/template-cloudfront.yaml +++ b/infra/cfn/template-cloudfront.yaml @@ -20,10 +20,18 @@ Resources: function handler(event) { var request = event.request; var uri = request.uri; - if (uri.endsWith("/")) { - request.uri += "index.html"; - } else if (uri === "") { + if (uri === "" || uri === "/") { request.uri = "/index.html"; + return request; + } + if (uri.endsWith("/")) { + request.uri = uri + "index.html"; + return request; + } + var lastSlash = uri.lastIndexOf("/"); + var lastDot = uri.lastIndexOf("."); + if (lastDot <= lastSlash) { + request.uri = uri + "/index.html"; } return request; }