

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# (選用) 配置網頁重新導向
<a name="how-to-page-redirect"></a>

如果您的 Amazon S3 儲存貯體配置為靜態網站託管，則您可配置儲存貯體或其中物件的重新引導。下列選項皆可供您設定重新引導。

**Topics**
+ [將儲存貯體網站端點的請求重新引導至其他儲存貯體或網域](#redirect-endpoint-host)
+ [配置重新引導規則以使用進階條件重新引導](#advanced-conditional-redirects)
+ [物件的重新引導請求](#redirect-requests-object-metadata)

## 將儲存貯體網站端點的請求重新引導至其他儲存貯體或網域
<a name="redirect-endpoint-host"></a>

您可以將對儲存貯體之網站端點的所有請求重新引導至其他儲存貯體或網域。如果您重新導向所有請求，對網站端點提出的任何請求都會重新引導至指定的儲存貯體或網域。

例如，若根網域為 `example.com`，而您想要處理對於 `http://example.com` 及 `http://www.example.com` 的要求，則必須建立兩個儲存貯體，並各自命名為 `example.com` 及 `www.example.com`。接著，維護 `example.com` 儲存貯體中的內容，再設定其他 `www.example.com` 儲存貯體，將所有請求重新導向至 `example.com` 儲存貯體。如需詳細資訊，請參閱[使用自訂網域名稱設定靜態網站](https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html)。

**重新導向儲存貯體網站端點的請求**

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在 **Buckets** (儲存貯體) 下，選擇要重新導向所有請求的來源儲存貯體名稱 (如 `www.example.com`)。

1. 選擇 **Properties** (屬性)。

1. 在 **Static website hosting** (靜態網站託管) 下，選擇 **Edit** (編輯)。

1. 選擇 **Redirect requests for an object** (重新導向物件請求)。

1. 在**主機名稱**方塊中，輸入儲存貯體或自訂網域的網站端點。

   例如，如果您要重新導向至根網域位址，您會輸入 **example.com**。

1. 在 **Protocol** (通訊協定) 中，選擇重新導向請求的通訊協定 (**無**、**http** 或 **https**)。

   如果您未指定通訊協定，則預設選項為**無**。

1. 選擇 **Save changes** (儲存變更)。

## 配置重新引導規則以使用進階條件重新引導
<a name="advanced-conditional-redirects"></a>

您可以使用進階重新導向規則，依據特定的物件金鑰名稱、要求的字首或回應碼，有條件地路由要求。例如，假設您要刪除或重新命名儲存貯體中的物件。您可以新增路由規則，將要求重新導向至另一個物件。若您想要將資料夾設為無法使用，可以新增路由規則，將要求重新導向至另一個網頁。您也可以新增路由規則處理錯誤條件，方法是在處理錯誤時，將傳回錯誤的要求路由到其他網域。

在啟用儲存貯體的靜態網站託管時，您可以選擇性地指定進階重新引導規則。Amazon S3 具有每個網站組態 50 個路由規則的限制。如果需要 50 個以上的路由規則，您可以使用物件重新導向。如需詳細資訊，請參閱「[使用 S3 主控台](#page-redirect-using-console)」。

如需使用 REST API 配置路由規則的詳細資訊，請參閱《Amazon Simple Storage Service API 參考》**中的 [PutBucketWebsite](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketWebsite.html)。

**重要**  
若要在新的 Amazon S3 主控台中建立重新導向規則，您必須使用 JSON。如需 JSON 範例，請參閱 [重新導向規則範例](#redirect-rule-examples)。

**設定靜態網站的重新導向規則**

若要為已啟用靜態網站託管的儲存貯體新增重新導向規則，請依照下列步驟進行。

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在左側導覽窗格中，選擇**一般用途儲存貯體**。

1. 在儲存貯體清單中，選擇已設定為靜態網站的儲存貯體名稱。

1. 選擇 **Properties** (屬性)。

1. 在 **Static website hosting** (靜態網站託管) 下，選擇 **Edit (編輯)**。

1. 在 **Redirection rules** (重新導向規則) 方塊中，輸入 JSON 中的重新導向規則。

   在 S3 主控台中，您可以使用 JSON 描述規則。如需 JSON 範例，請參閱 [重新導向規則範例](#redirect-rule-examples)。Amazon S3 具有每個網站組態 50 個路由規則的限制。

1. 選擇 **Save changes** (儲存變更)。

### 路由規則元素
<a name="configure-bucket-as-website-routing-rule-syntax"></a>

以下是在網站組態中定義路由規則的一般語法 (JSON 和 XML 格式)。若要在新的 S3 主控台中設定重新導向規則，您必須使用 JSON。如需 JSON 範例，請參閱 [重新導向規則範例](#redirect-rule-examples)。

------
#### [ JSON ]

```
[
    {
      "Condition": {
        "HttpErrorCodeReturnedEquals": "string",
        "KeyPrefixEquals": "string"
      },
      "Redirect": {
        "HostName": "string",
        "HttpRedirectCode": "string",
        "Protocol": "http"|"https",
        "ReplaceKeyPrefixWith": "string",
        "ReplaceKeyWith": "string"
      }
    }
  ]
 
Note: Redirect must each have at least one child element. You can have either ReplaceKeyPrefix with or ReplaceKeyWith but not both.
```

------
#### [ XML ]

```
<RoutingRules> =
    <RoutingRules>
         <RoutingRule>...</RoutingRule>
         [<RoutingRule>...</RoutingRule>   
         ...]
    </RoutingRules>

<RoutingRule> =
   <RoutingRule>
      [ <Condition>...</Condition> ]
      <Redirect>...</Redirect>
   </RoutingRule>

<Condition> =
   <Condition> 
      [ <KeyPrefixEquals>...</KeyPrefixEquals> ]
      [ <HttpErrorCodeReturnedEquals>...</HttpErrorCodeReturnedEquals> ]
   </Condition>
    Note: <Condition> must have at least one child element.

<Redirect> =
   <Redirect> 
      [ <HostName>...</HostName> ]
      [ <Protocol>...</Protocol> ]
      [ <ReplaceKeyPrefixWith>...</ReplaceKeyPrefixWith>  ]
      [ <ReplaceKeyWith>...</ReplaceKeyWith> ]
      [ <HttpRedirectCode>...</HttpRedirectCode> ]
   </Redirect>

Note: <Redirect> must have at least one child element. You can have either ReplaceKeyPrefix with or ReplaceKeyWith but not both.
```

------

下表說明路由規則中的元素。


|  名稱  |  描述  | 
| --- | --- | 
| RoutingRules |  容器，包含 RoutingRule 元素集合。 | 
| RoutingRule |  規則，可識別條件及符合條件時所套用的重新導向。 條件： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonS3/latest/userguide/how-to-page-redirect.html)  | 
| Condition |  容器，描述要套用指定的重新導向時所須符合的條件。若路由規則不含條件，便會將該規套用到所有要求。  | 
| KeyPrefixEquals |  物件金鑰的字首，而該物件金鑰是重新導向要求的起源。 若未指定 `KeyPrefixEquals`，即須指定 `HttpErrorCodeReturnedEquals`。若同時指定了 `KeyPrefixEquals` 及 `HttpErrorCodeReturnedEquals`，則兩者都必須為 true 才符合條件。  | 
| HttpErrorCodeReturnedEquals |  HTTP 錯誤代碼，套用重新導向時必須符合此代碼。發生錯誤時，若錯誤代碼符合此值，便會套用指定的重新導向。 若未指定 `HttpErrorCodeReturnedEquals`，即須指定 `KeyPrefixEquals`。若同時指定了 `KeyPrefixEquals` 及 `HttpErrorCodeReturnedEquals`，則兩者都必須為 true 才符合條件。  | 
| Redirect |  容器元素，提供重新導向要求的指示。您可以將要求重新導向至其他主機或頁面，或是指定使用其他協定。`RoutingRule` 必須具有 `Redirect` 元素。`Redirect` 元素至少須包含下列一個同級元素：`Protocol`、`HostName`、`ReplaceKeyPrefixWith`、`ReplaceKeyWith` 或 `HttpRedirectCode`。  | 
| Protocol |  回應傳回的 `http` 標頭中要使用的 `https` 或 `Location` 協定。 若已提供其同級項目，便無須 `Protocol`。  | 
| HostName |  回應傳回的 `Location` 標頭中要使用的主機名稱。 若已提供其同級項目，便無須 `HostName`。  | 
| ReplaceKeyPrefixWith |  物件金鑰名稱的字首，會取代重新導向要求中的 `KeyPrefixEquals` 值。 若已提供其同級項目，便無須 `ReplaceKeyPrefixWith`。只在未提供 `ReplaceKeyWith` 時才須提供。  | 
| ReplaceKeyWith |  回應傳回的 `Location` 標頭中要使用的物件金鑰。 若已提供其同級項目，便無須 `ReplaceKeyWith`。只在未提供 `ReplaceKeyPrefixWith` 時才須提供。  | 
| HttpRedirectCode |  回應傳回的 `Location` 標頭中要使用的 HTTP 重新導向代碼。 若已提供其同級項目，便無須 `HttpRedirectCode`。  | 

#### 重新導向規則範例
<a name="redirect-rule-examples"></a>

下列範例說明常見的重新導向工作：

**重要**  
若要在新的 Amazon S3 主控台中建立重新導向規則，您必須使用 JSON。

**Example 1：重新命名金鑰字首後重新導向**  
假設您的儲存貯體包含下列物件：  
+ index.html
+ docs/article1.html
+ docs/article2.html
您決定將資料夾從 `docs/` 重新命名為 `documents/`。當完成此變更後，您必須將字首為 `docs/` 的要求重新導向至 `documents/`。例如，`docs/article1.html` 的要求會重新導向至 `documents/article1.html`。  
在本案例中，您需要在網站組態中新增下列路由規則。  

```
[
    {
        "Condition": {
            "KeyPrefixEquals": "docs/"
        },
        "Redirect": {
            "ReplaceKeyPrefixWith": "documents/"
        }
    }
]
```

```
  <RoutingRules>
    <RoutingRule>
    <Condition>
      <KeyPrefixEquals>docs/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>
```

**Example 2：將對已刪除之資料夾的要求重新導向至頁面**  
假設您要刪除 `images/` 資料夾 (亦即刪除金鑰字首為 `images/` 的所有物件)。您可以新增路由規則，將對於所有金鑰字首為 `images/` 之物件的要求重新導向至名為 `folderdeleted.html` 的頁面。  

```
[
    {
        "Condition": {
            "KeyPrefixEquals": "images/"
        },
        "Redirect": {
            "ReplaceKeyWith": "folderdeleted.html"
        }
    }
]
```

```
  <RoutingRules>
    <RoutingRule>
    <Condition>
       <KeyPrefixEquals>images/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>folderdeleted.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>
```

**Example 3：重新導向至具有特定路徑的另一個網域**  
假設您想要將特定路徑的請求重新導向至另一個網域。例如，您想要將 `/redirect/me` 的請求重新導向至 `https://example.com/new/path`。  
同時使用 `HostName` 和 `ReplaceKeyWith` 時，Amazon S3 會透過將主機名稱和替換金鑰以正斜線連接起來，建構重新導向 URL。因此，您不應該在 `ReplaceKeyWith` 值中包含正斜線。Amazon S3 會自動在主機名稱和替換金鑰之間新增正斜線。  

```
[
    {
        "Condition": {
            "KeyPrefixEquals": "redirect/me"
        },
        "Redirect": {
            "HostName": "example.com",
            "ReplaceKeyWith": "new/path"
        }
    }
]
```

```
  <RoutingRules>
    <RoutingRule>
    <Condition>
      <KeyPrefixEquals>redirect/me</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <HostName>example.com</HostName>
      <ReplaceKeyWith>new/path</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>
```
此組態會將 `https://yourbucket.s3-website-region.amazonaws.com/redirect/me` 的請求重新導向至 `https://example.com/new/path`。請注意，`ReplaceKeyWith` 設定為 `new/path`，不含正斜線。

**Example 4：重新導向 HTTP 錯誤**  
假設找不到請求的物件時，您想要將請求重新導向至 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體。新增重新導向規則，以在傳回 HTTP 狀態代碼 404 (Not Found) (找不到) 時，將網站訪客重新導向至處理請求的 Amazon EC2 執行個體。  
下列範例也會在重新導向中，插入物件金鑰字首 `report-404/`。例如，若您請求頁面 `ExamplePage.html`，卻收到了 HTTP 404 錯誤，便會將該請求重新導向至指定之 Amazon EC2 執行個體的頁面 `report-404/ExamplePage.html`。若無任何路由規則，但發生了 HTTP 錯誤 404，將會傳回組態中指定的錯誤文件。  

```
[
    {
        "Condition": {
            "HttpErrorCodeReturnedEquals": "404"
        },
        "Redirect": {
            "HostName": "ec2-11-22-333-44.compute-1.amazonaws.com",
            "ReplaceKeyPrefixWith": "report-404/"
        }
    }
]
```

```
  <RoutingRules>
    <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals >
    </Condition>
    <Redirect>
      <HostName>ec2-11-22-333-44.compute-1.amazonaws.com</HostName>
      <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
    </Redirect>
    </RoutingRule>
  </RoutingRules>
```

## 物件的重新引導請求
<a name="redirect-requests-object-metadata"></a>

您可以在物件的中繼資料內設定網站重新導向位置，將物件要求重新導向至其他物件或 URL。只要將 `x-amz-website-redirect-location` 屬性新增到物件中繼資料，就能設定重新導向。在 Amazon S3 主控台中，您可以在物件的中繼資料內設定 **網站重新導向位置**。如果您使用 [Amazon S3 API](#page-redirect-using-rest-api)，則可以設置 `x-amz-website-redirect-location`。網站會將該物件解譯為 301 重新導向。

若要將要求重新導向至其他物件，必須將重新導向位置設為目標物件的金鑰。若要將要求重新導向至外部 URL，必須將重新導向位置設為所需的 URL。如需物件中繼資料的詳細資訊，請參閱「[系統定義的物件中繼資料](UsingMetadata.md#SysMetadata)」。

設定頁面重新導向時，可以保留或刪除來源物件內容。例如，若儲存貯體中有 `page1.html` 物件，您可以將任何對此頁面的要求重新導向至另一個物件 `page2.html`。您有兩種選擇：
+ 保留 `page1.html` 物件的內容並重新導向頁面要求。
+ 刪除 `page1.html` 的內容並上傳名為 `page1.html` 的零位元組物件，以取代現有物件及重新導向頁面要求。

### 使用 S3 主控台
<a name="page-redirect-using-console"></a>

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在 **Buckets** (儲存貯體) 清單中，選擇已配置為靜態網站的儲存貯體名稱 (例如 `example.com`)。

1. 在 **Objects** (物件) 下，選擇您的物件。

1. 選擇 **Actions** (動作)，然後選擇 **Edit metadata** (編輯中繼資料)。

1. 選擇 **Metadata** (中繼資料)。

1. 選擇 **Add Metadata** (新增中繼資料)。

1. 在 **Type** (類型) 下，選擇 **System Defined** (系統定義)。

1. 在 **Key** (金鑰) 中，選擇 **x-amz-website-redirect-location**。

1. 在 **Value** (值) 中，輸入您要重新導向至的物件金鑰名稱，例如 `/page2.html`。

   如果是同一儲存貯體中的其他物件，則此值的字首必須是 `/`。您也可以將值設為外部 URL，例如 `http://www.example.com`。

1. 選擇 **Edit metadata** (編輯中繼資料)。

### 使用 REST API
<a name="page-redirect-using-rest-api"></a>

下列 Amazon S3 API 動作支援請求中的 `x-amz-website-redirect-location` 標頭。Amazon S3 會在物件中繼資料中將標頭值儲存為 `x-amz-website-redirect-location`。
+ [PUT 物件](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html)
+ [啟動分段上傳](https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadInitiate.html)
+ [POST 物件](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html)
+ [PUT 物件 - 複製](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html)

設定為網站託管用的儲存貯體同時具有網站端點與 REST 端點。已設為 301 重新導向的頁面要求可能會有下列結果，視要求的端點而定：
+ **區域專用的網站端點 – **Amazon S3 會依據 `x-amz-website-redirect-location` 屬性的值重新導向頁面請求。
+ **REST 端點 – **Amazon S3 不會重新導向頁面請求，而會傳回所要求的物件。

如需端點的詳細資訊，請參閱「[網站端點與 REST API 端點之間的主要差異](WebsiteEndpoints.md#WebsiteRestEndpointDiff)」。

您可以在設定頁面重新導向時，決定要保留或刪除物件內容。例如，假設您的儲存貯體中有 `page1.html` 物件。
+ 若要保留 `page1.html` 的內容，並只重新導向頁面請求，您可以提交 [PUT 物件 - 複製](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html)請求，以現有的 `page1.html` 物件作為來源建立新的 `page1.html` 物件。在您的要求中設定 `x-amz-website-redirect-location` 標頭。當請求完成後，原始頁面及其內容保持不變，但 Amazon S3 會將所有對此頁面的請求重新導向至您指定的位置。
+ 若要刪除 `page1.html` 物件的內容，並重新導向頁面的請求，可以傳送 PUT 物件請求，上傳具有相同物件金鑰 `page1.html` 的零位元組物件。在 PUT 要求中，將 `x-amz-website-redirect-location` 的 `page1.html` 設為新物件。當要求完成後，`page1.html` 中不具任何內容，而要求會重新導向至 `x-amz-website-redirect-location` 指定的位置。

當您使用 [GET 物件](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html)動作擷取物件及其他物件中繼資料時，Amazon S3 會在回應中傳回 `x-amz-website-redirect-location` 標頭。