Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.example.com/MyDirectory

If I enter example.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?

like image 491
jremi Avatar asked May 31 '16 20:05

jremi


People also ask

What is the URL of static website hosted on S3 bucket?

To enable static website hosting Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to enable static website hosting for.

Can Amazon S3 run a static website?

You can use Amazon S3 to host a static website. On a static website, individual webpages include static content. They might also contain client-side scripts. By contrast, a dynamic website relies on server-side processing, including server-side scripts, such as PHP, JSP, or ASP.NET.


3 Answers

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.example.com
Static website hosting set to:
"Enable website hosting"

[S3 Bucket #2]
Bucket name: example.com
Static website hosting set to:
"Redirect all requests to another host name" pointing to www.example.com (Bucket #1)

With this configuration any of my web traffic coming in on the root domain can hit the Amazon S3 bucket container example.com and then auto redirect via Amazon to the primary S3 bucket www.example.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.example.com and not need to be redirected thru the example.com bucket.

who is running into issues with making your root domain traffic route correctly.

like image 107
jremi Avatar answered Oct 05 '22 14:10

jremi


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   example.com.s3-website-us-west-1.amazonaws.com.
URL Redirect Record   @     www.example.com  (unmasked)

S3 setup (only need 1 bucket)

[S3 Bucket]
Bucket name: www.example.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.example.com/*"
        }
    ]
}

This has allowed both example.com and www.example.com to work and my NameCheap email works as well.

like image 39
sirclesam Avatar answered Oct 05 '22 16:10

sirclesam


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:

  1. navigate to S3 dashboard
  2. click on the bucket you want to use (www.example.com)
  3. click on the 'permissions' nav bar tab
  4. a box should appear with two columns and an 'edit' button in the top right corner of the box - click that edit button
  5. there should be 4 total checkboxes - UNCHECK the bottom TWO
  6. 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"

like image 24
regs Avatar answered Oct 05 '22 16:10

regs