1
votes

I'm trying to setup the redirects such that all variations:
http://example.com, http://www.example.com, https://example.com
redirect to https://www.example.com

I've gone through several other posts and have it working except I'm noticing some weirdness in the redirects.

http://www.example.com -> 301 -> https://www.example.com Working as Intended

http://example.com -> 301 -> http://www.example.com -> 301 -> https://www.example.com
Scenario 1: Is there a way to skip the first 301?

https://example.com -> 301 -> http://www.example.com -> 301 -> https://www.example.com
Scenario 2: It actually redirects back to http first. Is there a way to skip the first 301?

Setup
S3
Two buckets:

  • www.example.com which hosts the site
  • example.com, which redirects to www.example.com with https protocol.

CloudFront
Two distributions:

  • Distribution one

    • custom origin: www.example.com.s3-website-us-west-2.amazonaws.com
    • alternate cname: www.example.com
    • viewer protocol policy: Redirect HTTP to HTTPS
  • Distribution two

    • custom origin: example.com.s3-website-us-west-2.amazonaws.com
    • alternate cname: example.com
    • viewer protocol policy: HTTP and HTTPS

Route 53
A and AAAA records for example.com and www.example.com each pointing to their respective Cloudfront distributions.

1

1 Answers

2
votes

for Distribution two

custom origin: example.com.s3-website-us-west-2.amazonaws.com alternate cname: example.com viewer protocol policy: Redirect HTTP to HTTPS

This way you can avoid this secanrio 2:

https://example.com -> 301 -> http://www.example.com -> 301 -> https://www.example.com

it will be like this

https://example.com -> https://www.example.com

Scenario 1 can avoid two times redirection but you can do like this

http://example.com -> 301 -> https://example.com -> 301 -> https://www.example.com

There is some limitation where you can't ignore 301 redirection while using cloudfront.