2

I'm hosting a static website on AWS and want the naked domain to redirect to www. For example, if the user enters example.com I want it to show as www.example.com.

I found this question which is the exact same question as mine, but I want to ask a few more specifics before I take my site offline to change this.

I followed the AWS tutorial to deploy a static website. So if I want my root bucket to redirect to the www bucket, I will deploy the HTML/CSS/JS files to the www bucket and then set the root bucket to redirect?

Lastly, when I set the bucket policy. This is how the tutorial explained to do it for the root domain:

{
  "Version":"2012-10-17",
  "Statement": [{
    "Sid": "Allow Public Access to All Objects",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::example.com/*"
  }
 ]
}

I wouldn't need this anymore and instead would put this on the www bucket. However, do I change the Resource to www.example.com/*?

{
  "Version":"2012-10-17",
  "Statement": [{
    "Sid": "Allow Public Access to All Objects",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::www.example.com/*"
  }
 ]
}

So it would be

"Resource": "arn:aws:s3:::www.example.com/*"

Is that how you would set it up?

eric
  • 123
  • 3
  • See this [answer](https://stackoverflow.com/questions/22053472/how-do-i-redirect-a-naked-apex-domain-to-www-using-route-53/69644219#69644219) and should solve your problem. – Diego Bianchi Oct 20 '21 at 10:24
  • There is a similar topic on Stack overflow, here's the [answer](https://stackoverflow.com/questions/22053472/how-do-i-redirect-a-naked-apex-domain-to-www-using-route-53/69644219#69644219) – Diego Bianchi Oct 20 '21 at 10:25

1 Answers1

2

Create your www.example.com bucket and set it up for hosting a static website. Apply your policy (pasted here for completion):

{
  "Version":"2012-10-17",
  "Statement": [{
    "Sid": "Allow Public Access to All Objects",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::www.example.com/*"
  }
 ]
}

The Resource property is updated with the current bucket.

Once that's in place, deploy your website to this bucket. Update your Route 53 records to point www.example.com to this bucket. You should then test www.example.com to ensure it's working. If not, fix it. Only when it's working should you continue.

Once the www.example.com is working, then you would modify your example.com bucket to simply redirect to www.example.com. No need to modify your Route 53 records since it's already pointing to your example.com bucket.

Once this is done, your browser should hit example.com and then be redirected to www.example.com.

When you're all done, you can delete all the objects from your example.com bucket since they're not being accessed anymore. You can also remove any custom permissions/policies on that bucket.

Matt Houser
  • 9,709
  • 1
  • 26
  • 25