2010-02-16 143 views
41

是否可以在s3對象上放置重定向標頭?像301重定向一樣。Amazon S3對象重定向

例如:

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 
+0

你跟這個細微的理想解決方案? – Daveo 2010-04-10 10:00:48

+1

此功能剛剛添加完成。請參閱下面的答案。 – Layke 2012-10-22 10:58:17

+0

你應該在這裏接受答案。 – Gray 2013-02-13 14:40:48

回答

33

在過去的一個月,這功能剛添加完成。

你可以在這裏找到API文檔:

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

,當你把你的目標,你需要設置針對對象,你希望使用301重定向的x-amz-website-redirect-location關鍵。

您也可以使用控制檯。

enter image description here

+1

這是否適用於任意對象?或只爲他們特殊的HTML靜態網站的東西? – Matchu 2015-09-22 04:16:47

+0

不幸的是,不支持通配符,所以我無法做出重定向來刪除我的靜態網站上的尾隨'/ index.html'。 – 2016-06-12 02:15:15

+0

如果在s3中使用cloudfront,這項工作是否可行? Cloudfront路由到我的網站,但隨後下載文件,而不是遵循我在s3中定義的重定向規則。 – Learner 2017-06-01 02:08:55

6

編輯:見回答如上這個功能現在在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

+0

我忘了提到你實際上會在重定向的HTML中創建一個頭標記。 – Layke 2010-02-16 12:40:04

+0

我寧願如果我可以在加載html之前在頭中重定向。我知道可以更改Content-Type的標題,但是我也可以設置重定向標題?就像這樣: HTTP/1.1 301永久移動 位置:http://www.example.org/ 內容類型:文本/ html 內容長度:174 – Martin 2010-02-16 12:47:51

+0

嗯不知道多行被禁用的評論。請參閱編輯的問題。 – Martin 2010-02-16 12:49:22

8

在情況下,如果網站託管是爲鬥啓用,有增加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-網站重定向的位置元數據他們。 壞消息是,對於一個桶,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, 如果網站託管被禁用,重定向支持不適用於此存儲桶。

+0

此功能是否支持正則表達式? : 我想爲所有不以文件擴展名結尾的文件添加「.html」。 例如:mysite.com/aaa/xxx應該被重定向到 - > mysite.com/aaa/xxx.html 我想將每個*重定向到.html,但只有當文件名不以一個文件擴展名。 (我不想把它做到js文件而不是文件夾...) 此功能是否支持正則表達式?有人可以用正則表達式發佈示例嗎? (KeyPrefixEquals^[^。] $ - > ReplaceKeyPrefixWith $ 1.html) – tamirko 2014-05-08 06:31:10

+0

它不支持正則表達式,所以這是不可能的 – 2016-06-12 06:04:21

2

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

1

AWSCLI可以讓你做到這一點很容易,現在(在某種程度上)

  1. 確保桶是100%的公衆
  2. 製作確定存儲區處於網站託管模式
  3. 僅引用使用它的網站的網址https://<bucket_name>.s3-website-<region>.amazonaws.com
  4. 使用s3api調用設置重定向

    AWS s3api把對象--acl公共閱讀--website重定向的位置「http://other-location.com」 --bucket富鬥--key某處/等等

注意:您不會發布任何對象到S3,因爲它只會提供301和重定向Location標頭。

測試與

curl -v 2 -L https://<bucket_name>.s3-website-<region>.amazonaws.com/somewhere/blah