dojogeorge
dojogeorge

Reputation: 1704

S3 Redirect all requests to index with request url as parameter

I am trying to set up an S3 bucket that handles a set of dynamic links and redirects them approriately. The structure of incoming links will be something like deeplink.s3-website-eu-west-1.amazonaws.com/link1, which Id like to direct to deeplink.s3-website-eu-west-1.amazonaws.com?url=link1. Is this possible within the redirect rules?

Upvotes: 2

Views: 1831

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179214

If you want S3 to redirect from /link1 to ?url=link1 then this will do it:

<RoutingRules>
 <RoutingRule>
  <Condition>
   <KeyPrefixEquals></KeyPrefixEquals>
   <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals>
  </Condition>
  <Redirect>
   <HostName>target.example.com</HostName>
   <ReplaceKeyPrefixWith>?uri=/</ReplaceKeyPrefixWith>
  </Redirect>
 </RoutingRule>
</RoutingRules>

The logic here is that if the key prefix is a zero-length string, which it always is -- the leftmost 0 characters of every string is the empty string, no matter what the string contains -- and the error code would have been 403, which it always is for missing objects if you haven't given the anonymous user permission to list objects (which you shouldn't) then prepend ?uri=/ to the key -- technically, we're asking S3 to do this by "replacing" the leftmost 0 characters in the key with our prepended string (also, remembering that S3 keys do not actually have a leading slash, so in my case, I add the / -- if you don't want the leading slash, then just use ?uri=) and redirect to the target host.

In the question, you mention redirecting back to the same bucket -- not sure what that will do for you, since S3 won't do anything with that query string, and in my application, I redirect to a different site -- but this routing rule does what you appear to be trying to do.


On the other hand, if you want S3 to redirect ?url=link1 to /link1, then that is impossible with routing rules, because they do not process the query string.

Upvotes: 7

Related Questions