# 配置重定向

如果文章的标题、版本或位置发生更改，则可以创建指向最新内容的重定向。

## 关于重定向

如果文章发生更改并影响用户能否找到它，我们会创建重定向，从任何过时的版本指向最新的内容。 当文章标题发生更改、添加新版本或文件的位置改变时，我们可能需要执行此操作。

可以在本地或外部配置重定向。

## 配置本地重定向

在 GitHub Docs 中，你可以从一个文件重定向到另一个文件，或从一个版本重定向到另一个版本。

### 跨文件重定向

如果更改文章的名称，并希望其旧 URL 重定向到所有版本的新 URL，请使用 `redirect_from` 前辅文和文章旧名称的路径。 如果更改文章的名称，并希望不同版本重定向到不同的 URL，请按照[跨版本重定向](#redirects-across-versions)部分中的步骤操作。

在以下示例中，文章《All about commits》被重命名为《Creating your first commit》。 `redirect_from` 前辅文将任何导航到旧文章 URL 的用户重定向到新文章 URL。

```yaml
title: Creating your first commit
redirect_from:
  - /content/get-started/all-about-commits
```

有关详细信息，请参阅 GitHub Docs 自述文件中的 [`redirect_from`](https://github.com/github/docs/blob/main/content/README.md#redirect_from)。

### 不包含版本的 URL 的自动重定向

如果输入的页面 URL 没有版本（`https://docs.github.com/ARTICLE` 而不是 `https://docs.github.com/VERSION/ARTICLE`），站点会自动将其重定向到页面的第一个可用版本。<!-- markdownlint-disable-line search-replace -->

优先级顺序在 [`lib/all-versions.ts`](https://github.com/github/docs/blob/main/src/versions/lib/all-versions.ts) 中指定。 当前优先级顺序为：

1. GitHub Free、GitHub Pro 或 GitHub Team (`fpt`)
2. GitHub Enterprise Cloud（`ghec`）
3. GitHub Enterprise Server (`ghes`)

如果标题为 `ARTICLE` 的页面仅在 GitHub Enterprise Cloud 和 GitHub Enterprise Server 中可用，则链接 `https://docs.github.com/ARTICLE` 将自动重定向到 `https://docs.github.com/enterprise-cloud@latest/ARTICLE`，因为 GitHub Enterprise Cloud 的优先级高于 GitHub Enterprise Server。<!-- markdownlint-disable-line search-replace -->

如果 `ARTICLE` 在免费、专业或团队版本中可用，则不会发生重定向，因为 `fpt` 页面没有版本段，所以将呈现 `fpt` 的 `https://docs.github.com/ARTICLE` 内容。<!-- markdownlint-disable-line search-replace -->

### 跨版本重定向

如果您希望文章某个版本的 URL 重定向到另一个版本的 URL 或完全重定向到另一个 URL，则必须更新 <c1> 目录中的 <c0>redirect-exceptions.txt</c0> 文件。

例如，如果你移除文章的免费、专业或团队 (`fpt`) 版本，URL 将自动重定向至页面的下一个可用版本。 如果你希望它重定向到优先级顺序较低的版本或完全不同的页面，则必须指定例外。

`redirect-exceptions` 文件中的每一项应以重定向\_终点\_的路径开头，其中包括版本，后接重定向\_起点\_路径的未排序列表。 在以下示例中，未排序列表中的路径将重定向至 [导出组织的成员信息](/zh/enterprise-cloud@latest/organizations/managing-membership-in-your-organization/exporting-member-information-for-your-organization) 的 GitHub Enterprise Cloud 版本。

```text
/enterprise-cloud@latest/organizations/managing-membership-in-your-organization/exporting-member-information-for-your-organization
  - /enterprise-server@3.3/organizations/managing-membership-in-your-organization/exporting-member-information-for-your-organization
  - /enterprise-server@3.4/organizations/managing-membership-in-your-organization/exporting-member-information-for-your-organization
  - /enterprise-server@3.5/organizations/managing-membership-in-your-organization/exporting-member-information-for-your-organization
```

## 配置外部重定向

极少数情况下，内容将移出 GitHub Docs 站点。 对于这些重定向类型，更新 `external-sites.json` 目录中的 [](https://github.com/github/docs/blob/main/src/redirects/lib/external-sites.json) 文件。

`external-sites.json` 文件中的每一项都是一个键值对，其中的键是内容的位置路径，值是它应重定向的终点路径。

```json
  "/github-status": "https://www.githubstatus.com/",
  "/articles/github-security": "https://github.com/security",
```