27

I have a website hosted on Amazon S3. It is the new version of an old website hosted on WordPress.

I have set up some files with the metadata Website Redirect Locationto handle old location and redirect them to the new website pages.

For example: I had http://www.mysite.com/solution that I want to redirect to http://mysite.s3-website-us-east-1.amazonaws.com/product.html So I created an empty file named solutioninside my bucket with the correct metadata:

Website Redirect Location= /product.html

The S3 redirect metadata is equivalent to a 301 Moved Permanentlythat is great for SEO. This works great when accessing the URL directly from S3 domain.

I have also set up a CloudFront distribution based on the website bucket. And when I try to access through my distribution, the redirect does not work, ie:

http://xxxx123.cloudfront.net/solution does not redirect but download the empty file instead.

So my question is how to keep the redirection through the CloudFront distribution ? Or any idea on how to handle the redirection without deteriorate SEO ?

Thanks

4 Answers 4

65

I ran into this problem recently and I found a workaround that seemed to work.

I created a Cloudfront distribution with a custom origin pointing to the S3 static website hostname instead of the bucket hostname. In the OP's case, the desired origin would be.

mysite.s3-website-us-east-1.amazonaws.com

Hitting a Cloudfront distribution just using the bucket as the origin does not work because the bucket does not actually serve redirects. It only serves files and stores metadata.

Hope that helps.

4
  • Note though that Cloudfront caches the redirect response instead of following it. Consequently downloads of resources you redirect to bypass Cloudfront.
    – tfischbach
    Jun 25, 2013 at 10:39
  • 3
    bmatsuo - If we could give you a billion rep points for this, we would. Thank you so much. You really saved the day.
    – snipe
    Jul 7, 2013 at 20:40
  • This helped a lot! Worth mentioning for anyone new to S3: to create a static bucket, go to your bucket and visit the Properties tab. Click Static Website Hosting. That will give you the correct URL which will allow for forwarding :)
    – rick6
    Jul 3, 2018 at 14:50
  • Unfortunately this requires your bucket to be public, which is a security problem.
    – joshwa
    Jul 8 at 19:35
3

Analysis

According to the documented Request and Response Behavior, and Supported HTTP Status Codes for Custom Origins, Amazon CloudFront doesn't follow Redirects, unfortunately:

[...] After you configure the redirect, the first time an end user submits a request for the object, CloudFront Front sends the request to the origin, and the origin responds with a redirect (for example, 302 Moved Temporarily). CloudFront caches the redirect and returns it to the end user. CloudFront does not follow the redirect. [emphasis mine]

Of course, you are using Amazon S3 rather than a custom origin, and a related section is notably absent from Request and Response Behavior for Amazon S3 Origins, but given Amazon S3 redirects have only been added fairly recently ( see Amazon S3 - Support for Website Redirects), it might just be missing there still.

Accordingly I venture the guess that you are not receiving an empty file with HTTP status code 200 OK, rather a HTTP status 301 Moved Permanently with no body at all - have you actually checked this with a browser or eventually only with a command line tool like e.g. cURL or HTTPie? The latter tools usually require an explicit parameter to follow redirects, so this might easily get unnoticed.

Potential Solution

If the analysis turns out to be correct, you'd need to configure the redirect to explicitly target CloudFront instead, again see Redirects:

You can configure your web server to redirect requests to one of the following locations:

  • The new URL of the object on the origin server. When the end user follows the redirect to the new URL, the end user bypasses CloudFront and goes straight to the origin. As a result, we recommend that you not redirect requests to the new URL of the object on the origin.

  • The new CloudFront URL for the object. When the end user submits the request that contains the new CloudFront URL, CloudFront gets the object from the new location on your origin, caches it at the edge location, and returns the object to the end user. Subsequent requests for the object will be served by the edge location. This avoids the latency and load associated with viewers requesting the object from the origin. However, every new request for the object will incur charges for two requests to CloudFront.

1
  • When I cURL the URL which have to be redirected through the CloudFront distribution I get : HTTP/1.0 200 OK Content-Type: application/octet-stream Content-Length: 0 My understanding is that the redirection is managed by S3 and in this case the file is hosted by CloudFront and it does not care about redirection headers set for S3 As S3 is the web server of the file I can not map the redirect other than with the metadata. Nov 22, 2012 at 11:13
1

Just to add - the "x-amz-website-redirect-location" header is seen in Lambda @ Edge (presumably also Cloudfront functions) You can use this to generate a 301/302 response if the header is present on the S3 response.

1
  • I have successfully used this method with a very simple lambda@edge origin_response function. The header is located at event['Records'][0]['cf']['response']['headers']['x-amz-website-redirect-location'][0]['value']
    – joshwa
    Jul 8 at 19:33
0

I realize it's been eight years, but in case anyone finds this, CloudFront connects with S3 via website mode (as outlined in the accepted answer) or fronting S3 in non-website mode using Origin Access Identity. I found myself here looking for a solution to the redirect that was working with S3 (website mode) after I switched to non-website mode; so, turning it back on won't work.

I found the following in the Amazon documentation: Redirect Functions. This function can be used to issue a redirect without even contacting the Origin Server.

1
  • This is good for basic redirects, but is no-go for more complex scenarios, as it only works currently with JS, has limitations such as file size (10 kB), or computation time which can increase quickly when having a few dozen redirects. AWS WishCloudFlare would have some NGINX or Cloudflare build-in redirects option.
    – goetzc
    Oct 28, 2021 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.