Forwarding www to non-www or non-www to www in AWS using s3 buckets cloudfront and route 53

Its normally seen as good practice to only have www.domain.com or domain.com serving content, this is because google see these as the different websites. this effects your SEO rankings because with two websites serving the same content google will mark them as duplicate content, which will result in a lower rank

The below will detail directing www.domain.com traffic to domain.com, but you can do it either way.

Pre-req, you will need to have your domain registered in Route 53, You will also need a SSL cert from 'Certificate Manager' which is free that has been verified

To direct all traffic to one or the other in AWS you will need

  • Two s3 Buckets
    • one that contains the website files
    • one that is empty and is used for website redirection
  • Two cloudfront distributions
    • one that points to the s3 bucket where your website is kept
    • one that points to the s3 bucket used for redirection
  • Two A records in route 53
    • One that points to the s3 bucket where your website is kept
    • One that points to the s3 bucket used for redirection

Setting up the Buckets

Website Bucket

I imagine if you're reading this you probably already have this one set up, the 'website bucket' is the s3 bucket where you are planning to host files for your glorious website You can call this somthing relevant like... 'websitebucket'

Make sure the bucket

  • Has 'static website hosting' enabled
  • Has the permissions and public access setup correctly
  • a relevant bucket policy to allow access
  • take note of the buckets website address (this is the one found near the static website settings)

Redirection bucket

create a second which will be empty, you can call this somthing like 'wwwredirectforwebsitebucket'

Make sure the bucket

  • Has 'static website hosting' enabled
  • 'Redirect requests for an object' selected
  • hostname is equal to the domain you want to forward traffic too (in my case domain.com)
  • take note of the buckets website address (this is the one found near the static website settings)

Setting up the Cloudfront

Website distribution

Create a distribution for the websitebucket In the 'Origin Domain' this is where you put the website endpoint that we previously took note of earlier (do not use the autofill!) Add the CNAME for ONLY the domain you want to direct traffic to (in my case domain.com)

Redirection distribution

Create a distribution for the websitebucket In the 'Origin Domain' this is where you put the website endpoint that we previously took note of earlier (do not use the autofill!) Add the CNAME for ONLY the domain you want to direct traffic from (in my case www.domain.com)

Setup the route53 records

Website A record

Create an A record that points the choosen domain to the website distribution (in my case domain.com)

Redirect A record

Create an A record that points the choosen domain to the redirect distribution (in my case www.domain.com)

That it, it should now be finished Note: dont forget to set 'redirect HTTP to HTTPS' in the cloudfront distribution for the website for an added bonus of directing http traffic to https.

Test it out, you should find everything will take you to https://domain.com - be ready for all of the bonus points from google...