Rewrite non www to www in AWS S3 bucket and CloudFront

Rewrite non www to www in AWS S3 bucket and CloudFront


1 min read

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\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

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

What is expectation ?

Redirect all request which comes to requests to

Answer :

  1. Create S3 bucket and set all code in this bucketBucket creation
  2. Create S3 bucket and set redirect to said by Second bucket creationRedirection setting
  3. Create CloudFront and configure with s3 bucket link of and add CNAME entry only for In route 53 for point alias as CloudFront link related to s3 bucket S3 origin configurationCNAME setting in CloudFront
  4. Do same setting as per point 3 for point alias s3 bucket of
  5. Create Route53 entry for both domain and A record and endpoint should be respective CloudFront endpoint. Route53 Configuration

Then we are done ๐Ÿฅณ ๐Ÿฅณ ๐Ÿš€๐Ÿš€๐Ÿš€

Reference :