Search code examples
amazon-s3amazon-cloudfront

AWS S3 with Cloudfront CDN for different files according to behavior query string


Currently I am serving some html pages on s3 with cloudfront. However, now I would like to serve a different index.html according to what I received in the parameter. For example: https://www.example.com?amp should serve an index.html different from the one already configured by default with https://www.example.com


Solution

  • You can do this using CloudFront Functions. Here is an example function you can use as a reference: https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/url-rewrite-single-page-apps

    Additional consideration: You should also create a cache policy that generates a different cache key based on the presence of that query string parameter, so that the presence of the query string returns different content from cache than when the query string is not present.