9

I know how to redirect/rewrite non-www to www using .htaccess in apache server. But I have no clue, about s3 bucket, and CloudFront. I have hosted the website on an s3 bucket using CloudFront.

How do I redirect all http://example.com/ requests to http://www.example.com

1
  • Welcome to Stack Overflow! Please provide more information so that we can better assist you. For example: Is example.com currently being served out of an Amazon S3 bucket? How are you using CloudFront? What have you tried so far? May 3, 2016 at 22:24

3 Answers 3

10
  1. Create a www.example.com S3 bucket and place all the code in this bucket
  2. Create a example.com S3 bucket and set redirect to www.example.com as mentioned in https://aws.amazon.com/blogs/aws/root-domain-website-hosting-for-amazon-s3/
  3. Create CloudFront and configure with S3 bucket link of www.example.com and add cname entry only for www.example.com.
  4. In Route 53 for www.example.com point alias as CloudFront link related to S3 bucket
  5. In Route 53 for example.com point alias S3 bucket of example.com
3
8

There is a feature in S3 where you can to this. Select a bucket, in Properties under Static Web Hosting select Redirect all requests to another host name.

Read more here: https://aws.amazon.com/blogs/aws/root-domain-website-hosting-for-amazon-s3/

Update from comment: Add a cname in your domain setup for example.com to point to your bucket endpoint and a cname for your www.example.com to point to the cloudfront endpoint.

4
  • This is not useful. i tried this. redirect all request to it just point to another bucket. May 4, 2016 at 8:49
  • It work only in case of s3 bucket and route 53 configuration. but if we are using cloudfront with this. then its now working. May 4, 2016 at 9:16
  • Then I am not sure if I understand your question. Add a cname in your domain setup for example.com to point to your bucket endpoint and a cname for your www.example.com to point to the cloudfront endpoint.
    – Gustaf
    May 4, 2016 at 10:25
  • The question clearly states Cloudfront. I cannot see how this is the answer. Did you add a second CloudFront Distribution for the base domain, it would be great if this answers this question because this question is not answered.anywhere. Jun 22 at 6:02
2

There are some ways to allow redirecting from non-www to www, the easiest way is

  1. Create two S3 buckets with name exact domain name (example.com and www.example.com)
  2. Enable bucket policy for public access
  3. Enable a static website for both domains, however, we will config redirect for example.com.
  4. Create two CloudFront for the two S3 buckets Create two alias A records for example.com and www.example.com and route them to the two CloudFront respectively.

Link reference: https://www.galireview.com/blog/static-website-on-amazon-s3-and-cloudfront/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.