CloudFront with S3 multi-site bucket - refresh page send back to root index

0

Hello,

I have a setup which does the following

  1. is upload a development branch to s3 bucket,
  2. upload a new root index.html with links links sub-domain to appropriate s3 folder

All this works ok, until i hit refresh (or even just copy and url and paste it into a new window). So i need to hir refresh and remain on the same page

======

Setup details:

  • S3 bucket with:
    • multiple sites hosted on it, each in it's own subdirectory
    • each sub-dir has an index.html
    • root s3 bucket has a index.html with links to each of those sub-dirc/index.html
  • CloudFront distribution pointing to S3 with:
    • origin access configured (and copied to s3)
    • alternate domains (wildcard subdomain & domain; all registered on Route 53 with SSL cert)
    • behaviour set with caching disabled, CORS-S3Origin, and the below CloudFunction for viewer request:
    • and a 404 error policy to revert to /index.html
function handler(event) {
    const domain = ".my-domain.com";

    var request = event.request;
    const subdomain = request.headers.host.value.split(domain);
    if (request.uri.length == 0 || request.uri.endsWith('/')) {
        if (request.uri.endsWith('/')) {
            request.uri += 'index.html';
        } else {
            request.uri += '/index.html';
        }
    }
    if (subdomain.length == 2) {
        request.uri = '/' + subdomain[0] + request.uri;  
    }
    return request;
}

Thanks

arnon
asked 21 days ago96 views
1 Answer
0

Hi

The issue you're facing is that refreshing the page or pasting the URL into a new window results in CloudFront potentially serving the object directly from its cache, bypassing your CloudFront function and the logic to rewrite the URI to the root index.html. Here's how to address this:

1. Leverage CloudFront Behaviors with Cache Key and Minimum TTL:

Refer repost: https://repost.aws/knowledge-center/prevent-cloudfront-from-caching-files

  • Create a separate CloudFront behavior specifically for your root domain (e.g., my-domain.com).
  • In this behavior configuration:Set "Cache Policy" to "Caching Disabled". This ensures CloudFront always fetches the latest version from S3.
  • Set "Minimum TTL" to a low value (e.g., 0 seconds).
  • Keep the existing behavior with the CloudFront function for handling subdomains.

2. Utlizie cache control headers

https://repost.aws/knowledge-center/cloudfront-cache-content

profile picture
EXPERT
GK
answered 21 days ago
  • thanks for answering GK,

    I already have Caching Disable for Default(*), but i have added what you recommended and now i have this screenshot

    and since i'm using Managed-CachingDisabled, TTL is already set to 0

    This did not help... when i refresh i still see the root index By the way, the url after refresh is subdomain.domain.com/some/url/here

  • Hi Look for the line request.uri = '/' + subdomain[0] + request.uri;. This line should be responsible for rewriting the URI for subdomains.

    1. Test your function from console or cli

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/test-function.html

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions