跳转到内容
搜索文档

HTML 处理

最后更新 查看 MarkdownAgent 设置

出于美观考虑,开发者通常希望控制请求路径是否强制或省略尾部斜杠(例如 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 处理

或者,如果你有特殊需求,可以完全禁用内置 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

这篇文档对您有帮助吗?