Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
Note though that Cloudfront caches the redirect response instead of following it. Consequently downloads of resources you redirect to bypass Cloudfront. –  tf. Jun 25 '13 at 10:39
    
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 '13 at 20:40
add comment

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.

share|improve this answer
    
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. –  Yannick Chaze Nov 22 '12 at 11:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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