Firebase Hosting仅部署index.html,其他页面404如何解决?
解决Firebase Hosting多页面HTML 404问题
核心原因
Firebase Hosting默认针对单页应用优化,当访问不带.html的路径或直接跳转子页面时,它不会自动映射到对应的HTML文件,从而触发404错误。
解决方案
方案1:启用干净URL(推荐)
如果你的页面链接使用不带.html的格式(比如<a href="/about">对应about.html),在firebase.json的hosting节点添加"cleanUrls": true配置,Firebase会自动将请求映射到同名的.html文件:
{ "hosting": { "public": "public", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "cleanUrls": true } }
方案2:配置路由重写
如果需要支持更复杂的路由规则(比如特定路径映射到指定页面),可以添加rewrites规则,将请求指向对应的HTML文件。例如,将/about指向about.html,/contact指向contact.html:
{ "hosting": { "public": "public", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "/about", "destination": "/about.html" }, { "source": "/contact", "destination": "/contact.html" } ] } }
额外检查项
- 确认
public目录下确实存在对应的HTML文件(比如about.html、contact.html),文件名大小写与链接完全一致(Firebase Hosting区分大小写)。 - 页面内的链接写法正确:使用相对路径(如
<a href="about.html">)或绝对路径(如<a href="/about.html">),避免错误的路径格式。
修改配置后,重新执行firebase deploy即可生效。
内容的提问来源于stack exchange,提问作者Charmalade
相关产品推荐
相关产品推荐

