I have an amazon S3 bucket serving a website from S3bucketurl.com/foo/index.html
.
I would like to configure a redirection rule, that automatically rewrite the URL as follows:
Accessing S3bucketurl.com
should automatically redirect to S3bucketurl.com/foo
, and serve the index.html
file contained in the subfolder foo
.
I tried to set up the Redirection Rule as such:
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyWith>api</ReplaceKeyWith>
</Redirect>
</RoutingRule>
</RoutingRules>
which results in a 404
:
404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: index.html
I've also tried permutations of <ReplaceKeyWith>/api</ReplaceKeyWith>
, <ReplaceKeyWith>api/</ReplaceKeyWith>
and <ReplaceKeyWith>/api/</ReplaceKeyWith>
to no avail.
<KeyPrefixEquals>/</KeyPrefixEquals>
There are two problems, here: first, this matches a prefix, meaning there's an implicit *
at the end. Thst isn't what you want, here. Also no key in an S3 bucket typically has a prefix of /
. The object key starts with the character after the leading slash. Your <ReplaceKeyWith>
is ignored because the prefix test never matches. But rules are not the best solution, here.
The way to do what you want is this:
Upload an empty file to create a new object in the root of the bucket, named whatever name you use for your index documents, typically index.html
. Make it public unless your bucket policy makes everything public.
In the object metadata for this object, set x-amz-website-redirect-location
to /api/
(or /foo/
, or whatever path you intend).