1

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.

Axiaz
  • 117
  • 1
  • 7

1 Answers1

2

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.

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
  • Changing that just creates a 3rd redirect for scenario 1 `http://example.com` -> `https://example.com` -> `http://www.example.com` -> `https://www.example.com` – Axiaz Dec 09 '19 at 09:25
  • it will be two redirection for scenario 1 and for 2 it will reduce to 1. Because when type https://example.com to https://www.example.com. Check this https://knabfinance.com in here http://redirectcheck.com/index.php – Avinash Dalvi Dec 09 '19 at 09:27
  • Yes that site you linked is how I want it to behave, but doing what you suggested doesn't work. Changing distribution 2 to Redirect HTTP to HTTPS does not affect scenario 2 and causes scenario 1 to create 3 redirects. Logically speaking it doesn't make sense for that to work since I'm trying to redirect from `https://example.com` to `https://www.example.com`, which is https to https, so I don't see why using HTTP to HTTPS would work at all. – Axiaz Dec 09 '19 at 09:33
  • Edit: I tried using the site you linked for checking redirects on my site and it matches the behavior that you described. However, using the network console in the browser shows the additional redirects happening. – Axiaz Dec 09 '19 at 09:38
  • give your domain to check what happening. In network console i am getting same behaviour. – Avinash Dalvi Dec 09 '19 at 09:39
  • Hm I just tried again and now the network console is showing the same behavior. Very confused, maybe Cloudfront hadn't fully updated yet? But seems like everything is working fine now. Thanks for the help! – Axiaz Dec 09 '19 at 10:00
  • cloudfront changes it took almost 30-60 minutes sometimes. Because they need to update to networking system. Cache behaviour sometime take time to reflect. – Avinash Dalvi Dec 09 '19 at 10:02