

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 重定向和重写示例参考
<a name="redirect-rewrite-examples"></a>

本节提供了有关多种常见重定向情景的示例。

**重要**  
特定于域的重定向不支持 source 字段中的路径组件。  
**支持**：  
`"source": "https://example.com" `（自动附加路径）
**不支持**：  
`"source": "https://example.com/specific-path"`
目前不支持使用 `domain+path` 组合的规则。
**替代模式**  
 对于特定于域的路径重定向，请使用：  
仅指定域的单独规则（系统会自动附加路径）
带条件逻辑的仅指定路径的规则
多规则的组合

您可以通过以下示例，来了解在 Amplify 控制台 JSON 编辑器中创建自己的重定向和重写的 JSON 语法。

**注意**  
原始地址域匹配不区分大小写。

**Topics**
+ [简单重定向和重写](#simple-redirects-and-rewrites)
+ [单页 Web 应用程序 (SPA) 的重定向](#redirects-for-single-page-web-apps-spa)
+ [反向代理重写](#reverse-proxy-rewrite)
+ [尾随斜线然后清理 URLs](#trailing-slashes-and-clean-urls)
+ [占位符](#placeholders)
+ [查询字符串和路径参数](#query-strings-and-path-parameters)
+ [基于区域的重定向](#region-based-redirects)
+ [在重定向和重写中使用通配符表达式](#wildcard-redirects)

## 简单重定向和重写
<a name="simple-redirects-and-rewrites"></a>

您可以使用以下示例将特定页面永久地重定向到新地址。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/original.html`   |   `/destination.html`   |   `permanent redirect (301)`   |  | 

JSON 格式

```
[
  {
    "source": "/original.html", 
    "status": "301", 
    "target": "/destination.html", 
    "condition": null
  }
]
```

您可以使用以下示例将一个文件夹下的任何路径都重定向到不同文件夹下的同一路径。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/docs/<*>`   |   `/documents/<*>`   |   `permanent redirect (301)`   |  | 

JSON 格式

```
[
  {
    "source": "/docs/<*>", 
    "status": "301", 
    "target": "/documents/<*>", 
    "condition": null
  }
]
```

您可以使用以下示例以重写的方式将所有流量都重定向到 index.html。在此情景中，重写使用户看来他们到达了原始地址。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/<*>`   |   `/index.html`   |   `rewrite (200)`   |  | 

JSON 格式

```
[
  {
    "source": "/<*>", 
    "status": "200", 
    "target": "/index.html", 
    "condition": null
  }
]
```

您可以使用以下示例来通过重写更改显示给用户的子域。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `https://mydomain.com`   |   `https://www.mydomain.com`   |   `rewrite (200)`   |  | 

JSON 格式

```
[
  {
    "source": "https://mydomain.com", 
    "status": "200", "target": "https://www.mydomain.com", 
    "condition": null
  }
]
```

您可以使用以下示例重定向到带有路径前缀的其他域。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `https://mydomain.com`   |   `https://www.mydomain.com/documents`   |   `temporary redirect (302)`   |  | 

JSON 格式

```
[
  {
    "source": "https://mydomain.com",
    "status": "302",
    "target": "https://www.mydomain.com/documents/",
    "condition": null
  }
]
```

您可以使用以下示例将某个找不到的文件夹下的路径重定向到自定义 404 页面。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/<*>`   |   `/404.html`   |   `not found (404)`   |  | 

JSON 格式

```
[
  {
    "source": "/<*>",
    "status": "404",
    "target": "/404.html",
    "condition": null
  }
]
```

**重要**  
不支持在基于域的源规则中使用路径组件（例如 `"https://domain.com/path"`），这会导致该规则被忽略且不会返回错误。

## 单页 Web 应用程序 (SPA) 的重定向
<a name="redirects-for-single-page-web-apps-spa"></a>

大多数 SPA 框架都支持 HTML5 History.pushState () 在不启动服务器请求的情况下更改浏览器位置。这适用于从根（或 */index.html*）开始的用户，但不适用于直接导航到任何其他页面的用户。

以下示例使用正则表达式将所有文件设置到 index.html 的 200 重写，但正则表达式中指定的特定文件扩展名除外。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `</^[^.]+$\|\.(?!(css\|gif\|ico\|jpg\|js\|png\|txt\|svg\|woff\|woff2\|ttf\|map\|json\|webp)$)([^.]+$)/>`   |   `/index.html`   |   `200`   |  | 

JSON 格式

```
[
  {
    "source": "</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json|webp)$)([^.]+$)/>",
    "status": "200", 
    "target": "/index.html", 
    "condition": null
  }
]
```

## 反向代理重写
<a name="reverse-proxy-rewrite"></a>

以下示例对来自其他位置的代理内容使用重写以便在用户看来域未更改。HTTPS 是唯一支持使用反向代理的协议。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/images/<*>`   |   `https://images.otherdomain.com/<*>`   |   `rewrite (200)`   |  | 

JSON 格式

```
[
  {
    "source": "/images/<*>", 
    "status": "200", 
    "target": "https://images.otherdomain.com/<*>", 
    "condition": null
  }
]
```

## 尾随斜线然后清理 URLs
<a name="trailing-slashes-and-clean-urls"></a>

为创建清洁网址结构（如 *about* 而不是 *about.html*），静态站点生成器（如 Hugo）会为具有 index.html (*/about/index.html*) 的页面生成目录。Amplify 在需要时 URLs 通过添加尾部斜杠来自动创建干净的效果。下表重点介绍了几种不同情景：


****  

| 用户在浏览器中的输入 | 地址栏中的网址 | 提供的文档 | 
| --- | --- | --- | 
|   `/about`   |   `/about`   |   `/about.html`   | 
|   `/about (when about.html returns 404)`   |   `/about/`   |   `/about/index.html`   | 
|   `/about/`   |   `/about/`   |   `/about/index.html`   | 

## 占位符
<a name="placeholders"></a>

您可以使用以下示例将一个文件夹结构中的路径重定向到另一个文件夹中的匹配结构。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/docs/<year>/<month>/<date>/<itemid>`   |   `/documents/<year>/<month>/<date>/<itemid>`   |   `permanent redirect (301)`   |  | 

JSON 格式

```
[
  {
    "source":  "/docs/<year>/<month>/<date>/<itemid>", 
    "status": "301",
    "target": "/documents/<year>/<month>/<date>/<itemid>",
               "condition": null
   }
]
```

## 查询字符串和路径参数
<a name="query-strings-and-path-parameters"></a>

**警告**  
请勿在路径或查询参数中 URLs 包含机密、凭证或敏感数据。这些值可在 Amplify 应用的访问日志中以明文方式查看。

您可以使用以下示例将路径重定向到其名称与原始地址中的查询字符串元素值匹配的文件夹：


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/docs?id=<my-blog-id-value`   |   `/documents/<my-blog-post-id-value>`   |   `permanent redirect (301)`   |  | 

JSON 格式

```
[
  {
    "source": "/docs?id=<my-blog-id-value>", 
    "status": "301",
    "target": "/documents/<my-blog-id-value>", 
    "condition": null
  }
]
```

**注意**  
Amplify 会将所有查询字符串参数转发到 301 和 302 重定向的目标路径。但是，如果原始地址包含设置为特定值的查询字符串（如本示例所示），Amplify 不会转发查询参数。在这种情况下，重定向仅适用于向具有指定查询值 `id` 的目标地址发出的请求。

您可以使用以下示例将在文件夹结构的给定级别上找不到的所有路径都重定向到指定文件夹中的 index.html。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/documents/<folder>/<child-folder>/<grand-child-folder>`   |   `/documents/index.html`   |   `not found (404)`   |  | 

JSON 格式

```
[
  {
    "source": "/documents/<x>/<y>/<z>", 
    "status": "404", 
    "target": "/documents/index.html", 
    "condition": null
  }
]
```

## 基于区域的重定向
<a name="region-based-redirects"></a>

您可以使用以下示例来基于区域重定向请求。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/documents`   |   `/documents/us/`   |   `temporary redirect (302)`   |   `<US>`   | 

JSON 格式

```
[
  {
    "source": "/documents", 
    "status": "302", 
    "target": "/documents/us/", 
    "condition": "<US>"
  }
]
```

## 在重定向和重写中使用通配符表达式
<a name="wildcard-redirects"></a>

您可以在原始地址中为重定向或重写使用通配符表达式 `<*>`。必须将表达式放在原始地址的末尾，而且该表达式必须唯一。Amplify 会忽略包含多个通配符表达式的原始地址，或者将其用在不同的放置中。

以下是使用通配符表达式的有效重定向示例。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/docs/<*>`   |   `/documents/<*>`   |   `permanent redirect (301)`   |     | 

以下两个示例展示了使用通配符表达式的*无效*重定向。


****  

| 原始地址 | 目标地址 | 重定向类型 | 国家/地区代码 | 
| --- | --- | --- | --- | 
|   `/docs/<*>/content`   |   `/documents/<*>/content`   |   `permanent redirect (301)`   |     | 
|   `/docs/<*>/content/<*>`   |   `/documents/<*>/content/<*>`   |   `permanent redirect (301)`   |     | 