Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward non WWW to WWW with AWS Amazon Cloud front behind HTTPS cloud front

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>
like image 780
Kamy D Avatar asked Mar 28 '16 15:03

Kamy D


3 Answers

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.

like image 69
Marcel Gruber Avatar answered Oct 02 '22 00:10

Marcel Gruber


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.

like image 37
Chris Pointon Avatar answered Oct 01 '22 23:10

Chris Pointon


You can redirect your naked domain under SSL to the "www" version of it (or any other) without any complicated DNS set-up or changing your provider by using https://www.nakedssl.com – it's free for one domain.

NakedSSL will create and host an SSL certificate for your naked domain (https://yourdomain.com), redirecting it to the HTTPS URL that you want (most likely "https://www.yourdomain.com").

I hope this is helpful for you!

Disclaimer: I'm part of the team that created NakedSSL.

like image 33
Jesús Avatar answered Oct 01 '22 22:10

Jesús