0
votes

I run a static website on an aws S3 bucket and have cloudfront in front of it to serve https.

Currently the website is only available through www.example.com (Which is what I want).

How can I setup a 301 redirect from non www to the www page?

Examples:

http://example.com =(301 Redirect)> https://www.example.com
https://example.com =(301 Redirect)> https://www.example.com
http://example.com/some-dir/info.jpg =(301 Redirect)> https://www.example.com/some-dir/info.jpg
1

1 Answers

0
votes

You can use Lambda@Edge to send a redirect for requests going to the apex. This requires creating a lambda function in the us-east-1 region with a code that checks the hostname the request is going to and conditionally return a redirect. Finally, you need to add it to the CloudFront distribution to the viewer request trigger point.

It's a bit more involved, but it can be done.

Here are some examples: