www.example.com doesn't work, but www.example.com/index.html does

0

Hello,

I am setting up a static website with s3 and cloudfront, and my site https://www.example.com/index.html loads just fine, but https://www.example.com doesn't - it throws a 403 error.

I have a hosted zone in route 53 for example.com with 6 records:

  1. example.com type A routing to my cloudfront distribution, similar to abcd123456789.cloudfront.net
  2. www.example.com type A again routing to my cloudfront distribution abcd123456789.cloudfront.net
  3. example.com type NS routing to my 4 nameservers, similar to ns-1234.awsdns-01.com
  4. example.com type SOA routing to the first nameserver of the NS record, so ns-1234.awsdns-01.com
  5. randomcharacters1.example.com type CNAME routing to randomcharacters2.tftwdmzmwn.acm-validation.aws
  6. randomcharacters3.www.example.com type CNAME routing to randomcharacters4.tftwdmzmwn.acm-validation.aws

The two CNAME records are pulled from aws CM > certificates > name of certificate > Domains, where it lists one of the records for example.com and the other for www.example.com.

Also, here is the complete list of what happens when I type these urls into the browser:

  1. example.com - redirects to https://example.com - 403 error
  2. www.example.com - redirects to https://example.com - 403 error
  3. https://example.com - 403 error
  4. https://www.example.com - 403 error
  5. http://example.com - redirects to https://example.com - 403 error
  6. http://www.example.com - redirects to https://example.com - 403 error
  7. example.com/index.html - redirects to https://example.com/index.html - displays correctly
  8. www.example.com/index.html - redirects to https://www.example.com/index.html - displays correctly
  9. https://example.com/index.html - displays correctly
  10. https://www.example.com/index.html - displays correctly
  11. http://example.com/index.html - redirects to https://example.com/index.html - displays correctly
  12. http://www.example.com/index.html - redirects to https://www.example.com/index.html - displays correctly

So it's the case that whenever I add /index.html the site works, but when I don't, it doesn't. Default root object under Cloudfront > Distributions > distribution name > General > Settings is example-com.s3.us-east-1.amazonaws.com/index.html. A link to an earlier related question I asked is here

4개 답변
2
수락된 답변

The default root object is configured incorrectly. According to the docs it should only be the object name relative to the bucket without any prefix /. In your case it should be index.html.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DefaultRootObject.html

AWS
전문가
답변함 일 년 전
  • It worked. I am eternally grateful.

0

You could use a CloudFront Function to append index.html to requests that don't specify a filename: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-index.html

profile pictureAWS
전문가
답변함 일 년 전
-1

I had a similar problem with the s3/cloud front configuration for my blog site. I had to use a custom origin in the cloudfront distribution.

If you look at https://headforthe.cloud/article/hosting-a-static-blog-in-aws/, it shares the terraform I used to deploy my site, specifically at https://headforthe.cloud/article/hosting-a-static-blog-in-aws/#cloudfront.

profile picture
답변함 일 년 전
-1
  • Make sure that you have set up the S3 bucket to act as a static website
  • Check if the "Origin Domain Name" is set to the S3 website endpoint (e.g., example-com.s3-website-us-east-1.amazonaws.com) instead of the bucket's REST endpoint (e.g., example-com.s3.us-east-1.amazonaws.com). If not, update the "Origin Domain Name" to the S3 website endpoint.
  • In the CloudFront Management Console, select your distribution and click on the "Distribution Settings" tab. Under "Default Root Object," set the value to "index.html" (without the bucket URL). This setting ensures that when users request the root URL of your website, CloudFront serves the "index.html" file. and Please Check

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.SimpleDistribution.html

profile picture
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠