2

I'm trying to setup redirect from the apex domain example.com to www.example.com with route53, cloudfront and s3.

I've seen many tutorials online and discussions here on the topic, but nothing has worked for me yet. And it's not clear exactly what records to create in route53.

Currently I have,

ALIAS    A    example.com    www.example.com

ALIAS A www.example.com abcdefg.cloudfront.net

As recommended in this setup video tutorial and many others.

This didn't work so I also tried to add a .htaccess with the following

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Which didn't work either.

Lots of people also suggest using a CNAME record, however it seems quite obvious to me that this doesn't work and mentioned in this answer Why can't a CNAME record be used at the apex (aka root) of a domain?.

Kaigo
  • 123

1 Answers1

2

Two concepts are being mixed. An ALIAS record in the DNS just affects how example.com resolves to an IP address. What you have done has exactly the same effect as

ALIAS    A    example.com        abcdefg.cloudfront.net
ALIAS    A    www.example.com    abcdefg.cloudfront.net

It won't actually redirect a web browser. Redirection would be caused by the web server.

You're on the right track with the .htaccess file, but typically only Apache web servers process the rules in a .htaccess file. S3 is not an Apache web server, and this link suggests that rules in .htaccess files on S3 are not processed.

If it were processed, the rules in your question are redirecting visitors from www.example.com to example.com, and your question suggests you want to do the opposite. So move the www. from the RewriteCond to the RewriteRule line. Again, if S3 is not looking at the rules in .htaccess, then whatever you put there won't result in the redirect you're aiming for.

You should probably investigate other methods of redirecting which don't use .htaccess, such as the method described here. Or if you need more comprehensive rule processing, use a more fully-featured HTTP server instead of S3.

tater
  • 1,475