Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 redirect 404 to different host

I am trying to get an S3 bucket when it encounters a 404 rather than throwing up a 404 page it redirects to my own server so I can then do something with the error.

This is what I have cobbled together, what I think it should do is go to mydomain.com and hit the error.php and let the php script workout the filename the user was trying to access on S3.

I would like this to happen no matter what directory the request comes from. When I have an error document defined in website hosting the 404 page shows up and when I don't have a 404 page defined I get an access denied xml error.

This is my current redirection rule

 <RoutingRules>
      <RoutingRule>
           <Condition>
                <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
           </Condition>
           <Redirect>
                <HostName>www.mydomain.com</HostName>
                <ReplaceKeyPrefixWith>error.php#!/</ReplaceKeyPrefixWith>
           </Redirect>
       </RoutingRule>
 </RoutingRules>

Can anyone give me a hint as to what I am missing please?

like image 341
Thickey Avatar asked Jan 23 '15 17:01

Thickey


1 Answers

Change the error code:

<HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals>

S3 doesn't generate a 404 unless the requesting user is allowed to list the bucket. Instead, it generates a 403 ("Forbidden"), because you're not allowed to know whether the object exists or not. In this case, that's the anonymous user, and you probably don't want to allow anonymous users to list the entire contents of your bucket.

If the object you request does not exist, the error Amazon S3 returns depends on whether you also have the s3:ListBucket permission.

If you have the s3:ListBucket permission on the bucket, Amazon S3 will return an HTTP status code 404 ("no such key") error.

if you don’t have the s3:ListBucket permission, Amazon S3 will return an HTTP status code 403 ("access denied") error.

— http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html

See also: Amazon S3 Redirect rule - GET data is missing

like image 158
Michael - sqlbot Avatar answered Sep 17 '22 16:09

Michael - sqlbot