mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
9e5444dbc7
Co-authored-by: locadex-agent[bot] <217277504+locadex-agent[bot]@users.noreply.github.com>
103 lines
2.3 KiB
Plaintext
103 lines
2.3 KiB
Plaintext
---
|
||
title: "重定向"
|
||
description: "为已移动、重命名或删除的页面配置重定向。"
|
||
keywords: ["301", "redirects"]
|
||
---
|
||
|
||
当你更改 docs 文件夹中文件的路径时,该页面的 URL 路径也会随之改变。这通常发生在重构文档或更改侧边栏标题时。
|
||
|
||
<div id="redirects">
|
||
## 重定向
|
||
</div>
|
||
|
||
<Note>
|
||
重定向**不能**包含像 `path#anchor` 这样的 URL 锚点,或像 `path?query=value` 这样的查询参数。
|
||
</Note>
|
||
|
||
在 `docs.json` 文件中添加 `redirects` 字段即可设置 301 重定向。
|
||
|
||
```json
|
||
"redirects": [
|
||
{
|
||
"source": "/source/path",
|
||
"destination": "/destination/path"
|
||
}
|
||
]
|
||
```
|
||
|
||
这会将 `/source/path` 永久重定向到 `/destination/path`,从而避免丢失原页面既有的 SEO(搜索引擎优化)。
|
||
|
||
<div id="wildcard-redirects">
|
||
### 通配符重定向
|
||
</div>
|
||
|
||
要匹配通配符路径,请在参数后面使用 `*`。在此示例中,`/beta/:slug*` 可匹配 `/beta/introduction`,并将其重定向到 `/v2/introduction`。
|
||
|
||
```json
|
||
"redirects": [
|
||
{
|
||
"source": "/beta/:slug*",
|
||
"destination": "/v2/:slug*"
|
||
}
|
||
]
|
||
```
|
||
|
||
<div id="partial-wildcard-redirects">
|
||
### 部分通配符重定向
|
||
</div>
|
||
|
||
使用部分通配符来匹配以特定前缀开头的 URL 片段。
|
||
|
||
```json
|
||
"redirects": [
|
||
{
|
||
"source": "/articles/concepts-*",
|
||
"destination": "/collections/overview"
|
||
}
|
||
]
|
||
```
|
||
|
||
这会匹配任何路径为 `/articles/concepts-` 的 URL,例如 `/articles/concepts-getting-started` 和 `/articles/concepts-overview`,并将它们全部重定向到 `/collections/overview`。
|
||
|
||
你也可以在目标地址中使用捕获到的通配符值进行替换。
|
||
|
||
```json
|
||
"redirects": [
|
||
{
|
||
"source": "/old/article-*",
|
||
"destination": "/new/article-*"
|
||
}
|
||
]
|
||
```
|
||
|
||
该重定向会将 `/old/article-123` 重定向到 `/new/article-123`,并在前缀之后保留捕获到的值。
|
||
|
||
<div id="avoid-infinite-redirects">
|
||
### 避免无限重定向
|
||
</div>
|
||
|
||
为避免出现无限循环,请不要创建路径彼此重定向的循环规则。
|
||
|
||
```json
|
||
"redirects": [
|
||
{
|
||
"source": "/docs/:slug*",
|
||
"destination": "/help/:slug*"
|
||
},
|
||
{
|
||
"source": "/help/:slug*",
|
||
"destination": "/docs/:slug*"
|
||
}
|
||
]
|
||
```
|
||
|
||
<div id="check-for-broken-links">
|
||
## 检查无效链接
|
||
</div>
|
||
|
||
使用 [CLI](/zh/installation) 检测无效链接。
|
||
|
||
```bash
|
||
mint broken-links
|
||
```
|