Amazon S3 static hosting with Namecheap DNS - How to correctly route non-www prefixed url
Solution 1:
I finally came to the following solution:
I am using Namecheap for the DNS and Amazon S3 bucket with static hosting enabled...
The solution enables both the ability to access the root domain and the www domain. In the namecheap advanced dns configuration I have the following settings:
CNAME Record @ domain.com.s3-website-us-west-1.amazonaws.com.
CNAME Record www www.domain.com.s3-website-us-west-1.amazonaws.com.
In the Amazon S3 configuration I have two separate buckets created.
[S3 Bucket #1]
Bucket name: www.domain.com
Static website hosting set to:
"Enable website hosting"
[S3 Bucket #2]
Bucket name: domain.com
Static website hosting set to:
"Redirect all requests to another host name" pointing to www.domain.com (Bucket #1)
With this configuration any of my web traffic coming in on the root domain can hit the Amazon S3 bucket container 'domain.com' and then auto redirect via Amazon to the primary S3 bucket 'www.domain.com' that serves all of my static content.
If someone attempts to connect directly on the www domain it will go directly to the Amazon S3 container 'www.domain.com' and not need to be redirected thru the 'domain.com' bucket.
Hopefully this will help anyone who is running into issues with making your root domain traffic route correctly.
Solution 2:
So I used jremi's answer but as mentioned below had issues with email being delivered. After some time with NameCheap support, I have a working solution.
NameCheap setup
CNAME Record www domain.com.s3-website-us-west-1.amazonaws.com.
URL Redirect Record @ www.domain.com (unmasked)
S3 setup (only need 1 bucket)
[S3 Bucket]
Bucket name: www.domain.com <---- This must match your domain exactly
Static website hosting set to:
"Enable website hosting"
S3 bucket Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::www.domain.com/*"
}
]
}
This has allowed both domain.com and www.domain.com to work and my NameCheap email works as well.
Solution 3:
I used sirclesam's method above and it got me 95% of the way there but my site still wouldn't load until I went and changed some configurations in my AWS S3 console.
Here are the steps to take:
- navigate to S3 dashboard
- click on the bucket you want to use (www.domain.com)
- click on the 'permissions' nav bar tab
- a box should appear with two columns and an 'edit' button in the top right corner of the box - click that edit button
- there should be 4 total checkboxes - UNCHECK the bottom TWO
- save and then try to access your site
This worked for me. I had initially followed a guide which only indicated that one (the third) checkbox needed to be checked but that resulted in my bucket still blocking access so unchecking the last checkbox was what fixed the issue.
For further clarification, the first checkbox (which should be UNCHECKED) reads: "Block public access to buckets and objects granted through new public bucket policies" and the second checkbox (which should ALSO be UNCHECKED)reads: "Block public and cross-account access to buckets and objects through any public bucket policies"