Amazon S3 Redirection Rule - No GET Data

We recently moved our site to Amazon S3 (all static pages). We moved the static files to another subdomain that still points to our old server.

Everything works just fine, except for one.

We got a script that is still being called from old clients in the main domain. On new clients, the URL is fixed, but old clients still use the old URL.

I found that we can manage redirects in Amazon S3, as described in http://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html . This works, except that we lose all of our GET variables.

Is there a way to detect S3 redirection with forwarding / saving GET variables? How can we fix this problem best? Any idea?

+3
redirect amazon amazon-s3 amazon-web-services
source share
2 answers

In the "GET data" section, it looks like you are referring to a query string - for example ?foo=1&bar=2 is the path to your URL. This is removed by S3 when you perform object level redirects.

Instead of redirecting certain objects, you can use the routing rules to redirect requests for all objects with a specific prefix to another website, and, if you wish, you can redirect it only if an error occurred while trying to service the original object (for example, 404). Using this method, the query string is sequentially saved.

To redirect every non-existent object from http://example.com/this/directory/* to http://other-site.example.com/that/directory/* , you should use a rule something like this:

 <RoutingRule> <Condition> <KeyPrefixEquals>this/directory/</KeyPrefixEquals> <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> </Condition> <Redirect> <Protocol>http</Protocol> <HostName>other-site.example.com</HostName> <ReplaceKeyPrefixWith>that/directory/</ReplaceKeyPrefixWith> </Redirect> </RoutingRule> 

<HttpErrorCodeReturnedEquals> not required if you want to redirect each request of the object corresponding to this prefix to another site, even if the object already exists in the bucket where the rule is configured. <ReplaceKeyPrefixWith> may be set to the same values ​​as <KeyPrefixEquals> , or may be completely omitted if you do not need to rewrite the path prefix.

Note that you do not use a slash in the parameters associated with the prefix.

Note that the correct value you want to use for <HttpErrorCodeReturnedEquals> may be 403 ("Forbidden") instead of 404 ("Not Found"). Objects in S3 can be made public either with permissions at the bucket level or at the object level. If the bucket itself is not publicly accessible, objects can still be, if their permissions are set individually. When the bucket itself is not publicly available, (iirc) S3 will not know whether or not an object exists, with 404 on demand without authentication and instead generates a 403 error, so there is an error in the redirection rules.

+6
source share

The key is to use ReplaceKeyPrefixWith instead of ReplaceKeyWith .

With Gulp and gulp-awspublish , for example, config is in JSON.

So, go from:

 Condition: KeyPrefixEquals: 'us.html' Redirect: ReplaceKeyWith: 'about.html' 

For

 Condition: KeyPrefixEquals: 'us.html' Redirect: ReplaceKeyPrefixWith: 'about.html' 

Redo example.com/us.html?a=1 redirects to example.com/about.htmk?a=1 .

Be careful with loops, for example.

 Condition: KeyPrefixEquals: 'about' Redirect: ReplaceKeyPrefixWith: 'about.html' 

Creates a redirect loop by adding .html.html.html , etc.

0
source share

All Articles