S3 Static Website RoutingRules when using Cloudfront and a domain name

0

Hello, I'm having a problem with my S3 RoutingRules on an S3 static website.

I've got an S3 bucket dev.example.com set as static website hosting. I have a Cloudfront distribution using the bucket as an origin, via the s3-website URL (not the REST URL from the dropdown selection). My domain name is registered in R53 and pointed to the CloudFront distribution URL.

All of that is working very nicely.

However, I've been trying to add a 301 redirect from one bucket object to another - I moved a page on my website.

So I've got my S3 bucket RoutingRules as follows:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>old-url</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyPrefixWith>new-url</ReplaceKeyPrefixWith>
      <HttpRedirectCode>301</HttpRedirectCode>
    </Redirect>
  </RoutingRule>
</RoutingRules>

When I visit dev.example.com/old-url, the redirect takes place, but takes me to dev.example.com.s3-website.eu-west-2.amazonaws.com/new-url - not to my domain name.

Is it possible to tell S3 to redirect to the correct domain name?

I have tried adding the full URL to the new page, i.e. <ReplaceKeyPrefixWith>https://dev.example.com/new-url</ReplaceKeyPrefixWith>

  • but this doesn't work, I just get the S3 URL with my full URL on the end!

Suggestions gratefully received.

asked 4 years ago792 views
1 Answer
0

Well it seems I rubber ducked myself.

Of course there's the HostName property.

Which works a treat.

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions