Amazon S3 static hosting with Namecheap DNS - How to correctly route non-www prefixed url

jremi picture jremi · May 31, 2016 · Viewed 17.3k times · Source

I have been reading other posts to try to get down to the bottom of this issue... but I need some clarification.

I am able to get all of my domain requests to hit my Amazon S3 bucket perfectly when entering www.FOO.com/MyDirectory

If I enter FOO.com/MyDirectory without the www it will fail.

What is the proper method to make url requests without the www route correctly to the same Amazon S3 bucket?

Any tips would help greatly. Thanks

Answer

jremi picture jremi · Jun 1, 2016

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.