跳至主要内容

配置重定向

如果文章的标题、版本或位置发生更改,您可以创建重定向以指向当前内容。

关于重定向

如果对文章进行的更改影响了人们查找它的方式,我们会在所有过时版本与当前内容之间创建重定向。如果文章标题更改、新增版本或文件位置更改,都可能需要这样做。

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

配置本地重定向

在 GitHub 文档中,您可以将一个文件重定向到另一个文件,或将一个版本重定向到另一个版本。

跨文件重定向

如果您更改了文章的名称,并希望其旧 URL 对所有版本都重定向到新 URL,请在 frontmatter 中使用 redirect_from 并提供文章旧名称的路径。如果您更改了文章的名称,并希望不同版本重定向到不同的 URL,请按照 跨版本重定向 部分中的步骤操作。

在下面的示例中,文章 “All about commits” 已重命名为 “Creating your first commit”。redirect_from frontmatter 会将访问旧文章 URL 的用户重定向到新文章 URL。

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

有关详细信息,请参阅 GitHub 文档 README 文件中的 redirect_from

未包含版本的 URL 的自动重定向

如果页面的 URL 未包含版本(https://docs.github.com/ARTICLE 而不是 https://docs.github.com/VERSION/ARTICLE),站点将自动将其重定向到该页面的第一个可用版本。

优先级顺序在 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。

如果 ARTICLE 在 Free、Pro 或 Team 中可用,则不会发生重定向,因为 fpt 页面没有版本段,所以位于 https://docs.github.com/ARTICLEfpt 内容会直接渲染。

跨版本重定向

如果您希望某个版本的文章的 URL 重定向到另一个版本的 URL,或完全重定向到其他 URL,则必须在 src/redirects 目录下更新 redirect-exceptions.txt 文件。

例如,如果您删除了文章的 Free、Pro 或 Team(fpt)版本,URL 将自动重定向到该页面的下一个可用版本。如果您希望它重定向到优先级顺序更低的版本,或完全重定向到其他页面,则必须指定例外。

redirect-exceptions 文件中,每条记录应以要重定向的目标路径(包括版本)开头,随后是一个无序列表,列出要重定向的来源路径。在下面的示例中,无序列表中的路径将重定向到 GitHub Enterprise Cloud 版本的 Exporting member information for your organization

/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 文档站点之外。对于此类重定向,请在 src/redirects 目录下更新 external-sites.json 文件。

external-sites.json 文件中的每个条目都是键值对,其中键是内容原始路径,值是目标重定向路径。

  "/github-status": "https://www.githubstatus.com/",
  "/articles/github-security": "https://github.com/security",
© . This site is unofficial and not affiliated with GitHub, Inc.