0

I have a simple HTML file that loads a json file that sits right next to it.

I followed some AWS docs here Implementing Default Directory Indexes since my website is structured like:

/apple/index.html /orange/index.html

and I need my users to be able to simply visit mydomain.com/apple without adding /index.html.

The Lambda@Edge (modified) function in AWS post works nicely, except for one key issue: each index.html file has a json file next to it and makes a same-origin request to load the json data.

This is fine if one visits /apple/index.html directly, but if you visit /apple the HTML file is unable to load the json file, it is getting 403 access denied because Lambda@Edge is modifying the request and basically appending the uri to itself.

How do I correct this in the Lambda function?

Here is the function I am actually using:

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  const uri = request.uri;

  if (uri.endsWith('/')) {
    request.uri += 'index.html';
  } else if (!uri.includes('.')) {
    request.uri += '/index.html';
  }

  callback(null, request);
};
Phil W
  • 11
  • 3

1 Answers1

1

Now that I have identified the root cause I have learned that this issue is common in Cloudfront.

This answer helped me solve the problem.

https://stackoverflow.com/a/49997450/13550178

Phil W
  • 11
  • 3