Amazon S3 Redirect rule - GET data is missing

前端 未结 2 1140
遥遥无期
遥遥无期 2020-12-09 23:38

We recently moved our website to Amazon S3 (all static pages). We moved al static files to a different subdomain which still points to our old server.

All is working

相关标签:
2条回答
  • 2020-12-10 00:36

    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.

    0 讨论(0)
  • 2020-12-10 00:44

    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.

    0 讨论(0)
提交回复
热议问题