【问题标题】:Amazon S3 object redirectAmazon S3 对象重定向
【发布时间】:2011-01-17 09:17:46
【问题描述】:

是否可以在 s3 对象上放置重定向标头?就像 301 重定向一样。

例如:

mybucket.amazon-aws.com/myobject --> example.com/test

最好通过在对象上设置这样的标题:

HTTP/1.1 301 Moved Permanently
Location: http://example.com/test
Content-Type: text/html
Content-Length: 0

【问题讨论】:

  • 您是否找到了理想的解决方案?
  • 刚刚添加了这个功能。请参阅下面的答案。
  • 你应该在这里接受答案。

标签: amazon-web-services amazon-s3 http-redirect


【解决方案1】:

在上个月内,刚刚添加了此功能。

您可以在此处找到 API 文档:

http://docs.amazonwebservices.com/AmazonS3/latest/dev/how-to-page-redirect.html

当您 PUT 对象时,您需要将该对象的 x-amz-website-redirect-location 键设置为您希望使用的 301 重定向。

您也可以使用控制台。

【讨论】:

  • 这适用于任意对象吗?还是只为他们特殊的 HTML-static-site 东西?
  • 不幸的是,不支持通配符,所以我无法进行重定向以删除静态站点上的尾随 /index.html
  • 如果将 cloudfront 与 s3 一起使用,这会起作用吗? Cloudfront 正在路由到我的站点,但随后下载文件而不是遵循我在 s3 中定义的重定向规则。
  • @Learner AFAIK 不,它需要通过 s3 网站端点访问。事实上,cloudfront 可能会剥离自定义 s3 标头。
【解决方案2】:

如果为存储桶启用网站托管,还有另一种添加 301 重定向的方法。 根据它,重定向规则以 XML 格式在存储桶级别进行描述,并且可以通过 AWS S3 控制台(静态网站托管部分)在存储桶的属性中指定。 目前可以在here找到有关其语法的完整文档。

当您有大量 URL 移动时,这种方法很方便,因为它更容易在一个地方管理所有重定向。例如可以定义重定向规则

<RoutingRule>
    <Condition>
        <KeyPrefixEquals>index.php</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
</RoutingRule>
<RoutingRule>
    <Condition>
        <KeyPrefixEquals>blog.php?id=21</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyWith>mysql-utf-8-database-creation-snippet.html</ReplaceKeyWith>
    </Redirect>
</RoutingRule>

这看起来与创建假对象并为它们指定 x-amz-website-redirect-location 元数据相同。 坏消息是一个存储桶的 XML 中这样的规则可能不超过 50 条。 是的,管理 XML 并不方便。但对我来说,这种方式目前更容易。同样,因为在一个地方管理所有文件更容易。

这种 XML 方法在您重命名包含大量页面的目录时非常有用。在这种情况下,有必要 为目录创建单个重定向规则,而不是为其中的每个页面创建单独的规则。比如

<RoutingRule>
    <Condition>
        <KeyPrefixEquals>blog/</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <ReplaceKeyPrefixWith>it-blog/</ReplaceKeyPrefixWith>
    </Redirect>
</RoutingRule>

根据这条规则example.com/blog/whatever会被重定向到example.com/it-blog/whatever

这种方法的另一个有用特性是它只替换前缀。与目录一样,它是可能的 重定向页面,但保存查询参数。如果这些查询有一些 JS 处理,它可能是合适的 参数。使用 x-amz-website-redirect-location 元数据,您可能会丢失它们。

正如我提到的,编写和阅读 XML 可能不方便。为了克服这个问题,我在 GWT 中写了a simple online tool 将具有以前和新 URL 的纯文本转换为 XML 格式。它使用 KeyPrefixEquals 谓词并执行ReplaceKeyPrefixWith 重定向。

最后根据documentation, 如果网站托管被禁用,则重定向支持不适用于此存储桶。

【讨论】:

  • 这个功能是否支持正则表达式? :我想在所有不以文件扩展名结尾的文件中添加“.html”。例如: mysite.com/aaa/xxx 应该重定向到 -> mysite.com/aaa/xxx.html 我想将每个 * 重定向到 .html,但前提是文件名不以文件扩展名结尾。 (我不想对 js 文件而不是文件夹这样做......)这个功能是否支持正则表达式?有人可以发布带有正则表达式的示例吗? (KeyPrefixEquals ^[^.]$ -> ReplaceKeyPrefixWith $1.html)
  • 它不支持正则表达式,所以这是不可能的
【解决方案3】:
【解决方案4】:

编辑:请参阅上面的答案,因为此功能现在是 AWS 中的原生功能

不是真的没有。 没有允许这样做的内置功能,但是,您可以做的是创建您的对象,即使您不将其保存为 HTML,您也可以将其应用为 HTML 文件。

例如:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Refresh" content="0; URL=http://www.example.com/target/">
    <title>Redirect</title>
</head>

<body>
    <a href="http://www.example.com/target/">http://www.example.com/target/</a>
</body>
</html>

在此处查看此页面:快速查看其上的源代码。

查看源代码:http://carltonbale.com.s3.amazonaws.com/distance_chart.png

你可以在这里看到一个解释:

第 8 点:http://carltonbale.com/9-hidden-features-of-amazon-s3

【讨论】:

  • 我忘了提到你实际上会在 HTML 中创建一个标题标签来重定向。
  • 我希望我可以在加载 html 之前在标头中重定向。例如,我知道可以更改 Content-Type 的标头,但我也可以设置重定向标头吗?像这样:HTTP/1.1 301 永久移动位置:example.org Content-Type:text/html Content-Length:174
  • 嗯不知道在 cmets 中禁用了多行。请参阅已编辑的问题。
【解决方案5】:

htaccess 301 到 Amazon S3 重定向翻译


htaccess 风格:

Redirect 301 /old/old.html http://www.new.com/new/new.html

翻译为:

<RoutingRule>
    <Condition>
        <KeyPrefixEquals>old/old.html</KeyPrefixEquals>
    </Condition>
    <Redirect>
        <HostName>www.new.com</HostName>
        <Protocol>http</Protocol>
        <HttpRedirectCode>301</HttpRedirectCode>
        <ReplaceKeyWith>new/new.html</ReplaceKeyWith>
    </Redirect>
</RoutingRule>

你可能可以写一个小脚本:

// -----------------
// Javascript
// htaccess to amazon s3 redirect for static s3 websites.
// (XML goes into S3 Bucket Properties Editor > [x] Enable website hosting > Edit Redirection Rules box.)
// -----------------

var list = [
    "Redirect 301 /old/old-1.html http://www.new.com/new/new-1.html",
    "Redirect 301 /old/old-2.html http://www.new.com/new/new-2.html",
    "Redirect 301 /old/old-3.html http://www.new.com/new/new-3.html"
];

var output = [];

output.push('<?xml version="1.0"?>');
output.push('<RoutingRules>');

for(var i=0; i<list.length; i++){

    var item = list[i].replace("Redirect 301 /", "").split(" ");
    var oldLoc = item[0];
    var newLoc = item[1];

    output.push("   <RoutingRule>");
    output.push("       <Condition>");
    output.push("           <KeyPrefixEquals>" + oldLoc + "/KeyPrefixEquals>");
    output.push("       </Condition>");

    output.push("       <Redirect>");
    if(newLoc.substr(0, 1) == "/"){

        output.push("           <ReplaceKeyWith>" + newLoc + "</ReplaceKeyWith>");

    } else {

        var splitNewLoc = newLoc.replace("http://", "").split("/");
        var host = splitNewLoc.shift();
        var path = splitNewLoc.join("/");

        output.push("           <HostName>" + host + "</HostName>");
        output.push("           <Protocol>http</Protocol>");
        output.push("           <HttpRedirectCode>301</HttpRedirectCode>");

        if(path){
            output.push("           <ReplaceKeyWith>" + path + "</ReplaceKeyWith>");
        }

    }

    output.push("       </Redirect>");
    output.push("   </RoutingRule>");

}

output.push('</RoutingRules>');

print(output.join("\n"));

警告:亚马逊将重定向规则的数量限制为 50 个。

【讨论】:

    【解决方案6】:

    AWSCLI 让你现在可以轻松地做到这一点(有点)

    1. 确保存储桶 100% 公开
    2. 确保存储桶处于网站托管模式
    3. 仅使用完整 URL https://&lt;bucket_name&gt;.s3-website-&lt;region&gt;.amazonaws.com 引用站点
    4. 使用s3api 调用来设置重定向

      aws s3api put-object --acl public-read --website-redirect-location "http://other-location.com" --bucket foo-bucket --key 某处/废话

    注意:您不要将任何对象发布到 S3,因为它只会提供 301 并重定向 Location 标头。

    测试

    curl -v 2 -L https://&lt;bucket_name&gt;.s3-website-&lt;region&gt;.amazonaws.com/somewhere/blah

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 2019-03-13
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多