Sal
Sal

Reputation: 492

Amazon S3 redirects not working

I have a static site hosted on S3 and I'd like to set a 301 redirect from www.example.com/2000/200/2/ to www.example.com.

I tried creating a directory structure:

/2000/200/2/index.html

and using S3's console to set a redirect in the Metadata properties, but this didn't work.

Next I tried a bucket rule that looked like:

<RoutingRules><RoutingRule>
<Condition><KeyPrefixEquals>http://www.example.com/2000/200/2/</KeyPrefixEquals></Condition>
<Redirect>
<HostName>www.example.com</HostName>
<Protocol>http</Protocol>
<ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule></RoutingRules>

But now when I go to the /2000/200/2/index.html I just see the page. So neither redirect worked! Any ideas?

Upvotes: 0

Views: 3851

Answers (2)

Sal
Sal

Reputation: 492

So two things:

First, this is the rule I used.

<RoutingRules>
<RoutingRule>
    <Condition>
        <KeyPrefixEquals>xx/x/xxxx/xxxxxx</KeyPrefixEquals>
        <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
        <HostName>www.example.com</HostName>
        <ReplaceKeyWith></ReplaceKeyWith>
    </Redirect>
</RoutingRule>
</RoutingRules>

Slightly different than Amazon's sample formatting, but it works.

Second I noticed that Firefox was removing this segment of the XML: "" after I save the snippet. Moved over to Chrome and no issues.

Lastly, I had to connect the site to the S3 website endpoint instead of the API endpoint, including in CloudFront.

Long story short, the AWS Static Site Tool works well, but if you use it as a starting point and then the complexity of your requirements increase, it'll have made some incorrect choices for you!

Upvotes: 1

John Rotenstein
John Rotenstein

Reputation: 269121

The Configure a Bucket for Website Hosting shows redirect examples such as:

  <RoutingRules>
    <RoutingRule>
    <Condition>
      <KeyPrefixEquals>docs/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>

and

  <RoutingRules>
    <RoutingRule>
    <Condition>
       <KeyPrefixEquals>images/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>folderdeleted.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>

These have the <KeyPrefixEquals> equal to the path relative to the root, rather than including http://www.example.com. Try matching that format.

Upvotes: 1

Related Questions