With Cloudfront we can define an "Origin Path" different from "/". It is useful when having multiple folders on a single S3 bucket for example.
Suppose you have many folders on a S3 bucket, websiteA, websiteB... A Cloudfront distribution is setup with "Origin Path" = /websiteA with URL websiteA.com. The origin of the distribution is the AWS webserver that sits on top of S3 (e.g websitesfolders.s3-website-us-east-1.amazonaws.com)
Suppose you create an HTML file on the S3 bucket at this location "/websiteA/foo/index.html"
When visiting websiteA.com/foo (without trailing "/"), S3 will try to fetch a resource called
/websiteA/foo. If unfound it will search if a file called
/websiteA/foo/index.html exists. If it does, it does not serve it but redirects 302 to it. When redirecting the Cloudfront distribution receives a request to
/websiteA/foo/index.html, it then appends the Origin Path to this, looking for
/websiteA/websiteA/foo/index.html which of course does not exist.
If we have an Origin Path other than /, and the origin is redirecting us with a relative link, this redirection from the Origin will contain the Origin Path already. Then adding again the Origin Path when querying the origin (what CloudFront does) really does look like a bug.
How could we get around this?
For now we used this redirection but it is pretty ugly and limited to 50 redirections per bucket:
<RoutingRule> <Condition> <KeyPrefixEquals>websiteA.com/websiteA/</KeyPrefixEquals> </Condition> <Redirect> <HostName>websiteA.com</HostName> <ReplaceKeyPrefixWith/> </Redirect> </RoutingRule>
Any ideas on how we could workaround this issue in a better way?