Rewrite non www to www in AWS S3 bucket and CloudFront

Avinash Dalvi
2 min readOct 13, 2020

Hello Everyone,

This article cover how to redirect non-www domain to www while using S3 bucket static hosting and CloudFront. As everyone might know about how to redirect/rewrite non-www to www using .htaccess in apache server.

RewriteEngine On 
RewriteBase / RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1

Like above apache rule but differently have to setup rules in S3 bucket and CloudFront.

What is expectation ?

Redirect all request which comes to http://example.com/ requests to http://www.example.com

Answer :

  • Create www.example.com S3 bucket and set all code in this bucket
  • Create example.com S3 bucket and set redirect to example.com said by
  • Create CloudFront and configure with s3 bucket link of example.com and add CNAME entry only for example.com. In route 53 for example.com point alias as CloudFront link related to s3 bucket
  • Do same setting as per point 3 for example.com point alias s3 bucket of example.com
  • Create Route53 entry for both domain www.example.com and example.com A record and endpoint should be respective CloudFront endpoint.

Then we are done πŸ₯³ πŸ₯³ πŸš€πŸš€πŸš€

Reference :

Originally published at https://www.internetkatta.com.

--

--

Avinash Dalvi

Enthusiastic learner, Full Stack Developer, Techno Savvy, Traveller, Out of Box thinker, Agile Lover, Problem Solver, Blogger