是否可以在 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
有帮助吗?

解决方案

在过去的一个月中,这个功能刚刚被添加。

您可以在这里找到API文档:

http://docs.amazonwebservices.com/ AmazonS3 /最新的/ dev /如何对页面将redirect.html

当你把你的目标,你需要设置针对对象,你希望使用301重定向的x-amz-website-redirect-location关键。

您也可以使用控制台。

“在这里输入的图像描述”

其他提示

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

当您进行大量 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 中 将带有旧 URL 和新 URL 的纯文本转换为 XML 格式。它使用KeyPrefixEquals 谓词和执行 ReplaceKeyPrefixWith 重定向。

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

修改:见回答如上这个功能现在在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-特征-亚马逊的--S3

<强> 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

AWSCLI 让你现在就可以轻松地做到这一点(某种程度上)

  1. 确保存储桶 100% 公开
  2. 确保存储桶处于网站托管模式
  3. 仅使用完整 URL 引用网站 https://<bucket_name>.s3-website-<region>.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://<bucket_name>.s3-website-<region>.amazonaws.com/somewhere/blah

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top