抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >


静态站点 URL 优化与部署配置:适配 Cloudflare Pages 和 Vercel 的路径方案解析

站点配置文件

_config.yml
pretty_urls:                            # 改写 permalink 的值来美化 URL
trailing_index: false # 是否在永久链接中保留尾部的 index.html # Set to false to remove trailing 'index.html' from permalinks
trailing_html: false # 是否在永久链接中保留尾部的 .html # Set to false to remove trailing '.html' from permalinks

通过禁用尾部的 index.html 和 .html 后缀,能让链接更简洁易读,例如将 /posts/hello-world.html 转换为 /posts/hello-world。

该配置用于适配 Cloudflare Pages。因为对于 Cloudflare Pages 直接访问/posts/hello-world.html 时会强制 308 重定向跳转到 /posts/hello-world。这会影响到 SEO,实际上已经影响到本站百度的 SEO 了,本站的解决方案是放弃百度,反正笔者也不用百度,它也只能搜到广告。已注销百度账号。

Cloudflare Pages: get rid of redundat 308 redirect 2021-11

Vercel 配置文件

source/vercel.json
{
"rewrites": [
{ "source": "/:path*", "destination": "/:path*" },
{ "source": "/:path*/", "destination": "/:path*/index.html" },
{ "source": "/:path*", "destination": "/:path*.html" }
]
}

这份 vercel.json 配置通过三条重写规则解决了静态网站部署到 Vercel 时的 URL 路由问题,确保不同格式的访问路径都能正确指向对应的 HTML 文件。其核心逻辑是按照规则优先级依次匹配请求路径,最终实现对无后缀 URL、带斜杠目录及传统 HTML 文件路径的兼容处理。

推荐阅读
安装配置 Service Api 安装配置 Service Api Hexo Hexo Django中引用静态文件 Django中引用静态文件 关于 Hexo 的文档链接 关于 Hexo 的文档链接 安装配置 Space 安装配置 Space 使用 Github Actions 部署 Hexo 使用 Github Actions 部署 Hexo

留言区

Are You A Robot?