8

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?

Thickey
  • 120
  • 1
  • 6
  • Thickey - Which path I need to write instead of "error.php#!" ? can you please elaborate this ? – Parthiv Aug 11 '16 at 13:54

1 Answers1

17

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

Community
  • 1
  • 1
Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • Aggghhh... it says 403 forbidden and everything.. it made total sense as soon as you mentioned it.. I'm an idiot.. I changed 404 for 403 and the redirect worked! Thanks so much! – Thickey Jan 26 '15 at 10:09
  • 1
    Is it possible to change this S3 behavior & return a default object when the requested object is not found without any kind of error ? – Rajat Gupta Feb 15 '15 at 06:30