出于美观考虑,开发者通常希望控制请求路径是否强制或省略尾部斜杠(例如 example.com/page/ 与 example.com/page)。此外,这也会影响 SEO,因为搜索引擎通常将带尾部斜杠和不带尾部斜杠的 URL 视为不同的独立页面。这种区别可能导致重复内容问题、索引问题,以及对页面正确规范版本的混淆。
assets.html_handling 配置 决定 HTML 内容请求的重定向(redirect)和重写(rewrite)。它用于指定规范 URL 的模式,从而确定 Cloudflare 从何处提供 HTML 内容,以及将非规范 URL 重定向到何处。
以下目录结构为例:
- dist
- file.html
- folder
- index.html
这通常会自动提供你期望的行为:单个文件(例如 foo.html)_不带_尾部斜杠提供,文件夹 index 文件(例如 foo/index.html)_带_尾部斜杠提供。
{
"name": "my-worker",
// Set this to today's date
"compatibility_date": "2026-08-17",
"assets": {
"directory": "./dist/",
"html_handling": "auto-trailing-slash"
}
}name = "my-worker"
# Set this to today's date
compatibility_date = "2026-08-17"
[assets]
directory = "./dist/"
html_handling = "auto-trailing-slash"根据传入请求,将提供以下资产:
| 传入请求 | 响应 | 提供的资产 |
|---|---|---|
| /file | 200 | /dist/file.html |
| /file.html | 307 to /file | - |
| /file/ | 307 to /file | - |
| /file/index | 307 to /file | - |
| /file/index.html | 307 to /file | - |
| /folder | 307 to /folder/ | - |
| /folder.html | 307 to /folder | - |
| /folder/ | 200 | /dist/folder/index.html |
| /folder/index | 307 to /folder | - |
| /folder/index.html | 307 to /folder | - |
或者,你可以强制使用尾部斜杠(force-trailing-slash)。
{
"name": "my-worker",
// Set this to today's date
"compatibility_date": "2026-08-17",
"assets": {
"directory": "./dist/",
"html_handling": "force-trailing-slash"
}
}name = "my-worker"
# Set this to today's date
compatibility_date = "2026-08-17"
[assets]
directory = "./dist/"
html_handling = "force-trailing-slash"根据传入请求,将提供以下资产:
| 传入请求 | 响应 | 提供的资产 |
|---|---|---|
| /file | 307 to /file/ | - |
| /file.html | 307 to /file/ | - |
| /file/ | 200 | /dist/file.html |
| /file/index | 307 to /file/ | - |
| /file/index.html | 307 to /file/ | - |
| /folder | 307 to /folder/ | - |
| /folder.html | 307 to /folder/ | - |
| /folder/ | 200 | /dist/folder/index.html |
| /folder/index | 307 to /folder/ | - |
| /folder/index.html | 307 to /folder/ | - |
或者,你可以省略尾部斜杠(drop-trailing-slash)。
{
"name": "my-worker",
// Set this to today's date
"compatibility_date": "2026-08-17",
"assets": {
"directory": "./dist/",
"html_handling": "drop-trailing-slash"
}
}name = "my-worker"
# Set this to today's date
compatibility_date = "2026-08-17"
[assets]
directory = "./dist/"
html_handling = "drop-trailing-slash"根据传入请求,将提供以下资产:
| 传入请求 | 响应 | 提供的资产 |
|---|---|---|
| /file | 200 | /dist/file.html |
| /file.html | 307 to /file | - |
| /file/ | 307 to /file | - |
| /file/index | 307 to /file | - |
| /file/index.html | 307 to /file | - |
| /folder | 200 | /dist/folder/index.html |
| /folder.html | 307 to /folder | - |
| /folder/ | 307 to /folder | - |
| /folder/index | 307 to /folder | - |
| /folder/index.html | 307 to /folder | - |
或者,如果你有特殊需求,可以完全禁用内置 HTML 处理(none)。
{
"name": "my-worker",
// Set this to today's date
"compatibility_date": "2026-08-17",
"assets": {
"directory": "./dist/",
"html_handling": "none"
}
}name = "my-worker"
# Set this to today's date
compatibility_date = "2026-08-17"
[assets]
directory = "./dist/"
html_handling = "none"根据传入请求,将提供以下资产:
| 传入请求 | 响应 | 提供的资产 |
|---|---|---|
| /file | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /file.html | 200 | /dist/file.html |
| /file/ | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /file/index | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /file/index.html | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /folder | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /folder.html | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /folder/ | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /folder/index | 取决于 not_found_handling 设置 |
取决于 not_found_handling 设置 |
| /folder/index.html | 200 | /dist/folder/index.html |