Yuda
Yuda

Reputation: 343

AWS Cloudfront redirects TOO_MANY times

I folowed Redirecting Internet Traffic to Another Domain and Redirecting HTTP Requests to HTTPS.

This is my status.

s3 : 3 buckets for web hosting

1) example.com(index.html in it, has policy),

2) www.example.com(for request redirection, no policy, redirect to example.com)

3) bucket-for-redirection(for cloudfront, no policy, redirect to example.com, https protocol)

cloudfront: 1 CloudFront

Route 53 Type A for 2 domain

1) example.com: Alias target is CloudFront

2) www.example.com: Alias target is s3

But my site returns ERR_TOO_MANY_REDIRECTS. Is there something i missed?


solution

  1. I removed all the buckets except one(bucket-for-redirection).

  2. Put the resources(ex. index.html) in there.

  3. create the bucket policy.

    {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
    ]
    

    }

  4. Then, set its properties with "Static Web Hosting", select first option, and type 'index.html' or else.

  5. Make sure the Alias's target is CloudFront in Route 53.

(If you're Korean, please refer to my blog.)

Upvotes: 4

Views: 8764

Answers (1)

John Hanley
John Hanley

Reputation: 81454

This problem is caused by too many redirects. This means that the user is going to one URL, then the user is sent to another URL and then sent to another URL, ... The web browser detects multiple redirects and displays an error the user. Otherwise it is possible for the user to get into a loop constantly moving from one URL to another and never displaying the desired web page.

There is not enough information in your question about how you have configured S3 and CloudFront, so I will explain how to determine the exact problem.

To debug this problem use curl.

Let's say that your goal is that all users go to https://www.example.com. Now verify that this url does not redirect. Note: some webservers will redirect a DNS name to a DNS name + the home page url. If this is the case test with the home page url (second command).

curl -i https://www.example.com > data.txt

OR (replace with your home page url):

curl -i https://www.example.com/index.html > data.txt

Now open the file data.txt in an editor. The first line should be HTTP/1.1 200 or HTTP/2 200. The key is the 200 (anything between 200 and 299). If instead the number is 301 (Moved Permanently) or 307 (Temporary Redirect), then you are redirecting the user. See my example below. This is most likely the problem. The key is to figure out why your desired DNS name is redirecting and to what it is redirecting to. Then find the configuration file / service that is redirecting incorrectly.

If the previous command is working correct, then test the other supported DNS names and see if they are redirecting correctly to your desire DNS name (https://www.example.com). A common problem is that the redirects are going to the wrong desired page which then loops back and forth redirecting.

Your goal is that the webserver returns the following (which includes both the HTTP headers and HTML body). The important items are the status code (301 or 307) and the redirect location (5th line below). The HTML body is ignored for redirects.

Example correct redirect for everything but the desired DNS name:

HTTP/2 301
date: Fri, 08 Mar 2019 04:17:18 GMT
server: Apache
x-frame-options: SAMEORIGIN
location: https://www.example.com/
content-length: 232
content-type: text/html; charset=iso-8859-1
via: 1.1 google
alt-svc: quic=":443"; ma=2592000; v="46,44,43,39"

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.example.com/">here</a>.</p>
</body></html>

Use curl and test all supported possibilities:

curl -i http://www.example.com

This should redirect to https://www.example.com

curl -i http://example.com

This should redirect to https://www.examle.com

curl -i https://example.com

This should redirect to https://www.examle.com

Repeat the above tests using your home page url and a few sub pages.

A common problem that I see even on correctly running websites is that the user is redirected more than once. Properly designed redirects should send the user to the correct location in one step, not in multiple steps. Multiple redirects slow down getting to the correct page.

Upvotes: 3

Related Questions