TIL · AWS

Fixing CloudFront trailing-slash 404s on S3 origins

2 min Jun 3, 2026 awscloudfronts3

S3 static website hosting serves index.html for directory-style URLs, but the S3 REST API origin (the *.s3.amazonaws.com endpoint) does not — so /articles/foo/ returns a 404 through CloudFront.

Two fixes:

  • Point CloudFront at the S3 website endpoint (*.s3-website-...) instead of the REST endpoint, or
  • Add a CloudFront Function that rewrites /foo//foo/index.html.
function handler(event) {
  var req = event.request;
  if (req.uri.endsWith("/")) req.uri += "index.html";
  return req;
}

For now NXGCloud uses the plain S3 website endpoint, so this isn’t needed yet.