6

I already have Cloud Front setup to redirect all calls to HTTPS protocol. Now I want to forward "non www" to www. How can I achieve this with respect to the current setup?

I tried the one below, but it wasn't successful as this one doesn't take HTTPS redirection into consideration.

https://www.pbxdom.com/how-redirect-non-www-to-www-with-amazon-cloud-front-route-53-and-s3

URL doesn't redirect www. Instead, it shows error message: This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
3
  • Can you clarify the case that's not working after following those instructions? Mar 28, 2016 at 15:52
  • URL doesn't redirect www. Instead, it shows error message: This XML file does not appear to have any style information associated with it. The document tree is shown below. Error> <Code>AccessDenied</Code> <Message>Access Denied</Message>
    – Kamy D
    Mar 31, 2016 at 22:09
  • Seems like your origin bucket isn't setup properly for redirection. Make sure your origin bucket is setup for a website and will redirect to www. Mar 31, 2016 at 22:57

2 Answers 2

7

Here's another way of solving the problem. I actually tried to do exactly what you did, and I could never seem to truly redirect the non wwww to the www when CloudFront was involved.

Once you set CloudFront to redirect all HTTP to HTTPS, it handles all of that redirection so you don't need to worry about it. You can take it out of the logic equation.

You should have 4 "A" type records in your Route 53 for the domain (assuming you have IPv6 enabled in CloudFront):

  • www.domain.com "A" alias record pointing to your CloudFront instance
  • www.domain.com "AAAA" alias record pointing to your CloudFront instance
  • domain.com "A" alias record pointing to your CloudFront instance
  • domain.com "AAAA" alias record pointing to your CloudFront instance

And lastly, In the CloudFront "Alternate Domain Names (CNAMEs)" field in the general CloudFront configuration, make sure it contains the www and the non www. Do note that CloudFront changes actually take quite a while to propagate, so be deliberate and patient with your changes.

The end result of this is that both www and non www will work, without any apparent redirection from non www to www, but there is still HTTPS redirection.

2
  • 7
    While it is true that this works in practice, this is not ideal from a SEO standpoint. It's best practice to force a redirect to either www or non-www, whichever is the canonical.
    – holaymolay
    Sep 11, 2018 at 15:55
  • Yes, that is certainly an important consideration, @brianjason! Sep 11, 2018 at 20:52
4

I have two CloudFront distributions to achieve this, one serving "example.com" for non-www to www redirection, and the second serving actual content from "www.example.com".

To do this I added a CloudFront Edge Lambda function on the non-www CloudFront distribution that redirects all requests to their www equivalent. I wrote a blog post with the details.

For an alternative approach without using Lambda, this answer points the non-www distribution to an S3 bucket set to redirect all requests to the www version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.