Amazon S3 Redirect rule - GET data is missing

孤街浪徒 提交于 2019-11-29 02:41:28

By "GET data," it sounds like you are referring to the query string -- e.g. ?foo=1&bar=2 -- that follows the path in your URL. This is removed by S3 when you do object-level redirects.

Instead of redirecting specific objects, you can use routing rules to redirect requests for all objects with a specific prefix to a different web site, and optionally you can do this redirect only if an error (such as 404) would occur while trying to serve the original object. Using this method, the query string seems to be consistently preserved.

To redirect every nonexistent object at http://example.com/this/directory/* to http://other-site.example.com/that/directory/* you'd 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> is not required if you want to redirect every object request matching that prefix to the other site, even when the object already exists in the bucket where the rule is configured. <ReplaceKeyPrefixWith> could be set to the same values as <KeyPrefixEquals> or possibly omitted entirely, if you don't need to rewrite the path prefix.

Note that you don't use a leading slash in the prefix-related options.

Note also that the correct value you'll want to use for <HttpErrorCodeReturnedEquals> may be 403 ("Forbidden") instead of 404 ("Not Found"). Objects in S3 can be made public by either bucket-level or object-level permissions. If the bucket itself is not publicly-readable, the objects still can be, if their permissions are set individually. When the bucket itself is not publicly-readable, (iirc) S3 will not acknowledge whether the object exists or not, with a 404 on an unauthenticated request, and will instead generate the 403 error, so that's the error you'll need to trap in the redirect rules.

The key is to use ReplaceKeyPrefixWith instead of ReplaceKeyWith.

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

So going from:

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

To

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

Will make example.com/us.html?a=1 redirect to example.com/about.htmk?a=1.

Be careful of loops, eg.

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

Will make a redirect loop by adding .html.html.html etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!