Add CloudFront Function for directory index routing
- Create CloudFront Function to rewrite directory requests to index.html - Fix admin login page routing issue (/upadaj -> /upadaj/index.html) - Attach function to CloudFront distribution default cache behavior - Enables proper routing for all admin pages without .html extension
This commit is contained in:
@@ -40,6 +40,15 @@ resource "aws_cloudfront_origin_access_control" "frontend" {
|
|||||||
signing_protocol = "sigv4"
|
signing_protocol = "sigv4"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# CloudFront Function for directory index rewrites
|
||||||
|
resource "aws_cloudfront_function" "index_rewrite" {
|
||||||
|
name = "${var.app_name}-index-rewrite"
|
||||||
|
runtime = "cloudfront-js-1.0"
|
||||||
|
comment = "Rewrite directory requests to index.html"
|
||||||
|
publish = true
|
||||||
|
code = file("${path.module}/cloudfront-function.js")
|
||||||
|
}
|
||||||
|
|
||||||
# CloudFront distribution
|
# CloudFront distribution
|
||||||
resource "aws_cloudfront_distribution" "frontend" {
|
resource "aws_cloudfront_distribution" "frontend" {
|
||||||
enabled = true
|
enabled = true
|
||||||
@@ -74,6 +83,11 @@ resource "aws_cloudfront_distribution" "frontend" {
|
|||||||
default_ttl = 3600 # 1 hour
|
default_ttl = 3600 # 1 hour
|
||||||
max_ttl = 86400 # 24 hours
|
max_ttl = 86400 # 24 hours
|
||||||
compress = true
|
compress = true
|
||||||
|
|
||||||
|
function_association {
|
||||||
|
event_type = "viewer-request"
|
||||||
|
function_arn = aws_cloudfront_function.index_rewrite.arn
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Custom error responses for SPA routing
|
# Custom error responses for SPA routing
|
||||||
|
|||||||
17
terraform/cloudfront-function.js
Normal file
17
terraform/cloudfront-function.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
function handler(event) {
|
||||||
|
var request = event.request;
|
||||||
|
var uri = request.uri;
|
||||||
|
|
||||||
|
// Check whether the URI is missing a file extension
|
||||||
|
if (!uri.includes('.')) {
|
||||||
|
// Check if URI ends with /
|
||||||
|
if (uri.endsWith('/')) {
|
||||||
|
request.uri += 'index.html';
|
||||||
|
} else {
|
||||||
|
// Add /index.html to directory paths
|
||||||
|
request.uri += '/index.html';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user