先说结论
Firebase Hosting 默认不会把 /about 映射到 about.html。不加配置时,链接必须写 /about.html。
打开 cleanUrls 之后:
/about会读取about.html;/about.html会 301 到/about;- 目录页面是否带结尾斜杠由
trailingSlash决定。
firebase.json 配置
{
"hosting": {
"public": "public",
"cleanUrls": true,
"trailingSlash": true,
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
}
}public指向包含index.html的目录;cleanUrls: true去掉.html;trailingSlash: true让目录地址以/结尾,例如/products/;- 多页站点不要再加
rewrites。
单页应用(SPA)才需要 rewrite
只有 React / Vue 这类单页应用,刷新子路由时需要回退到 index.html:
{
"hosting": {
"public": "dist",
"cleanUrls": true,
"rewrites": [{ "source": "**", "destination": "/index.html" }]
}
}多页静态站点加了这条规则,会把所有页面都变成首页,不要混用。
常见坑
- 没开
cleanUrls,站内却写了/about:直接 404。 - 开了
cleanUrls,站内还写about.html:多一次 301,canonical 容易写错。 public指向了源码目录而不是构建产物目录:部署后没有index.html。- 文件名大小写不一致:Firebase 区分大小写。
- 多页站点误加 SPA rewrite。
检查清单
- [ ]
firebase.json中cleanUrls: true - [ ]
public目录里有index.html - [ ] 站内链接和 canonical 不带
.html - [ ] 多页站点没有 SPA rewrite
- [ ] 需要目录页时统一使用带
/的地址 - [ ] 有
404.html
可直接复制的提示词
你是一名前端工程师,要为【品牌名 / 业务】生成一个可直接部署到 Firebase Hosting 的静态多页网站。
硬性规范:
1. 输出 public/index.html 和每个页面一个 HTML 文件,例如 public/about.html、public/products.html;资源放在 public/assets/。
2. 所有站内链接写不带 .html 的根相对路径:/、/about、/products、/contact;禁止写 about.html 或 ../。
3. canonical、og:url、sitemap.xml、JSON-LD 使用同样不带 .html 的地址。
4. 同时输出 firebase.json,包含 public、cleanUrls: true、trailingSlash: true;多页站点不要添加 rewrites。
5. 文件名只用小写字母、数字和连字符。
6. 不依赖服务端渲染、Node 或数据库;只用 HTML、CSS 和原生 JavaScript。
7. 提供 public/404.html。
8. 输出完整文件清单和每个文件的完整内容,不要省略。
9. 页面正文使用【中文 / 英文】。