在 S3 路由规则中重定向主页

Redirect homepage in S3 Routing Rules

I'm using S3 routing rules to deprecate an old domain, routing the traffic to a new domain. So, the old domain was product.com, and the new location is my-organization.com/product/.

I am using routing rules to redirect pages. So, I have a rule like this that redirects the FAQ:

    <Condition>
        <KeyPrefixEquals>faq</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <Protocol>https</Protocol>
        <HostName>my-organization.com</HostName>
        <ReplaceKeyPrefixWith>product/faq</ReplaceKeyPrefixWith>
        <HttpRedirectCode>301</HttpRedirectCode>
    </Redirect>

This works well!

The one thing left to do is redirect the old homepage to the product page. I've tried using / to match the homepage:

<KeyPrefixEquals>/</KeyPrefixEquals>

But it doesn't seem to match anything. And I've tried matching on the empty key:

<KeyPrefixEquals></KeyPrefixEquals>

But that matches everything. Is there a way to just redirect the homepage in S3 routing rules? What I want is something like:

    <Condition>
        <KeyPrefixEquals>/</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <Protocol>https</Protocol>
        <HostName>my-organization.com</HostName>
        <ReplaceKeyPrefixWith>product/</ReplaceKeyPrefixWith>
        <HttpRedirectCode>301</HttpRedirectCode>
    </Redirect>

谢谢!

桶的根不是/,它是空字符串。一个空的键前缀不会完全符合你的要求,因为所有东西都有一个匹配空字符串的前缀(因为 LEFT(${some_string},LENGTH('')) == '',不管 ${some_string} 的内容如何),所以这样的规则不会在任何情况下,只匹配 根。它会匹配所有内容。

没有办法只专门重定向根页面,但这是我一直在使用的解决方法,效果很好。将此作为您的最后一个路由规则(因为它们是按顺序匹配的,如果那里还没有页面的话,这个确实匹配所有尚未匹配的内容)。

<RoutingRule>
    <Condition>
        <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyPrefixWith>product/</ReplaceKeyPrefixWith>
        <Protocol>https</Protocol>
        <HostName>new-site.example.com</HostName>
    </Redirect>
</RoutingRule>

您不应启用 public 存储桶列表,但如果启用,则需要将 403 替换为 404。如果您有索引文档,则需要将其删除。

本质上,对于任何因该路径缺少 public 页面而被拒绝的请求,都会附加 product/,因此 http://bucket.example.com/ 变为 https://new-site.example.com/product/

同样,http://bucket.example.com/some/random/file.txt会重定向到https://new-site.example.com/product/some/random/file.txt,但是旧页面已经失效,所以新页面也会失效,所以lost/nothing没有任何收获。由于规则是按顺序匹配的,因此如果您最后添加此规则,它应该不会破坏任何其他内容。

或者,根据您实际需要执行的宏伟计划,一旦您从存储桶中删除旧内容,此规则实际上可能是您唯一需要的重定向规则。