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
  • 4

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,395
  • 2
  • 9
  • 12
  • still not working. Gave it a few days too. – Kaigo Aug 03 '20 at 10:25
  • Are you sure Cloudfront/S3 is processing the `.htaccess` file? If you are sure this is happening (e.g. by redirecting *all* requests) then please show your revised `.htaccess` file. – tater Aug 03 '20 at 10:52
  • I created an invalidation for the file to clear the cache. here's the file. ```RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]``` Thanks – Kaigo Aug 03 '20 at 11:10
  • I wasn't referring to caching. S3 is not an Apache web server, so the question is whether your `.htaccess` file even gets processed or if it is completely ignored. At least previously it was not processed (see https://stackoverflow.com/questions/14095818/amazon-s3-and-htaccess). – tater Aug 03 '20 at 11:27
  • I see yep, it is being processed. I couldn't see any solutions from the link that applied to me. I tried the build in s3 redirect, but that didn't work either. – Kaigo Aug 13 '20 at 13:46
  • Can you show the `.htaccess` file that you tested which did successfully do something? – tater Aug 13 '20 at 13:52
  • It's the same as the comment above (Aug 3rd @ 11:10). It is reachable via the link, `example.com/.htaccess` and downloads that file. However, the url is never redirected and stays on the apex domain. – Kaigo Aug 13 '20 at 14:08
  • If you can download it, that suggests S3 is treating it as a regular file. Your `.htaccess` rules should not be visible to the public. Basically my suspicion is that S3 does not process *any* rules in a `.htaccess` file (see link in above comment) and so far there hasn't been any evidence to the contrary. If the `.htaccess` file is not being processed, then it will be impossible to do such a redirect using `RewriteRule`. You'd need to either give up on the redirect; see if CloudFront itself offers redirection; or move to a proper web server. – tater Aug 13 '20 at 14:19
  • 1
    I added a new link in the answer which shows an approach not using `.htaccess`. – tater Aug 13 '20 at 14:30