# 使用 Cloudflare 配置子目录

{% stepper %}
{% step %}

#### 配置你的 GitBook 站点

在你的 GitBook 组织中，点击侧边栏中的 docs 站点名称，然后点击 **管理站点** 或打开 **设置** 选项卡。打开 **域名和重定向** 部分，在“子目录”下点击 **设置子目录**.

输入你希望托管文档的网站 URL。然后指定用于访问文档的子目录，例如 `tomatopy.pizza/docs`，然后点击 **配置**.

在 **附加配置**，你现在会看到一个代理 URL。接下来在配置 Cloudflare worker 时会用到它。将其复制到剪贴板。
{% endstep %}

{% step %}

#### 创建你的 Cloudflare worker

登录你的 Cloudflare 账户并导航到 **Workers & Pages**

点击 **创建** 按钮。

在“创建应用程序”页面上，点击 **Hello world** 卡片中的“从模板开始”按钮。

给这个 worker 起一个更具描述性的名称，例如 `mydocs-subpath-proxy`。完成重命名 worker 后，点击 **部署**.
{% endstep %}

{% step %}

### 配置你的自定义域名

你的 worker 会获得一个可用的默认 URL。要改为配置你的自定义域名（例如 `tomatopy.pizza`），点击 **设置。** 然后，在“Domains & Routes”部分中，点击 **+ 添加**.

在打开的“Domains & Routes”抽屉中，点击 **自定义域名**，然后在随后出现的文本框中输入你的自定义域名。当你指定自定义域名时， *不要* 包含子目录。例如， `tomatopy.pizza` 是正确的，而 `tomatopy.pizza/docs` 则不正确。
{% endstep %}

{% step %}

#### 更新 worker 代码

当 worker 完成部署后，点击 **编辑代码**，或者点击 **继续到项目**，然后点击 **编辑代码** 右上角的按钮。

在打开的代码编辑器中，将示例代码替换为以下代码片段：

{% code lineNumbers="true" %}

```javascript
export default {
  fetch(request) { 
    const SUBDIRECTORY = '/docs';
    const url = new URL(request.url);
    const target = "<INSERT YOUR PROXY URL FROM GITBOOK>" + url.pathname.slice(SUBDIRECTORY.length);
    const proxy = new URL(
      target.endsWith('/') ? target.slice(0, -1) : target 
    )
    proxy.search = url.search;
    return fetch(new Request(proxy, request));
  }
};
```

{% endcode %}

{% hint style="info" %}
请务必将第 5 行的 URL 更新为你在第一步中从 GitBook 获得的代理 URL。
{% endhint %}

完成后，点击 **部署**。此过程可能需要一些时间。完成后，访问该 URL 时，你应该会看到你的文档站点！
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/docs-site/custom-domain/setting-a-custom-subdirectory/configuring-a-subdirectory-with-cloudflare.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
