Firebase自定义域名www子域名无法返回index.html求助
Firebase Hosting www子域名根路径404问题解决
问题详情
- 配置两个自定义域名:
colademonofrozen.cl(正常返回index.html)和www.colademonofrozen.cl - www子域名根路径访问时触发404错误,但带路径的地址(如
www.colademonofrozen.cl/anything)可正常显示index.html - 已排除缓存、DNS传播问题(配置超两周),DNS服务商处无法设置跳转
- 产品已印刷www域名,需紧急修复
当前Firebase Hosting配置:
"hosting": { "public": "dist", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "/api/**", "function": "app", "region": "us-central1" }, { "source": "/admin", "destination": "/admin.html" }, { "source": "**", "destination": "/index.html" } ] },
解决方案
1. 新增www根路径专属重写规则
Firebase的**匹配规则可能未覆盖www子域名的根请求,在重写规则最顶部添加针对性规则:
{ "source": "/", "destination": "/index.html", "host": "www.colademonofrozen.cl" }
修改后完整配置:
"hosting": { "public": "dist", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "/", "destination": "/index.html", "host": "www.colademonofrozen.cl" }, { "source": "/api/**", "function": "app", "region": "us-central1" }, { "source": "/admin", "destination": "/admin.html" }, { "source": "**", "destination": "/index.html" } ] },
执行部署命令:
firebase deploy --only hosting
2. 配置Firebase域名重定向
若重写规则无效,直接在控制台设置永久重定向:
- 进入Firebase Hosting页面,找到
www.colademonofrozen.cl域名 - 点击右侧菜单,选择「添加重定向」
- 来源填
www.colademonofrozen.cl/**,目标填https://colademonofrozen.cl/$1,选择301永久重定向 - 保存后等待几分钟生效
3. 检查本地部署文件
确认dist目录下存在index.html,本地预览验证:
firebase serve --only hosting
可通过本地hosts映射www.localhost到127.0.0.1,测试根路径访问是否正常。
4. 确认Firebase域名状态
检查控制台域名配置,确保www.colademonofrozen.cl状态为「已部署」,无DNS错误提示。
验证方法
- 直接访问
www.colademonofrozen.cl,查看是否返回正常页面 - 用curl命令检查响应:
curl -I https://www.colademonofrozen.cl
确认状态码为200,响应指向index.html
内容的提问来源于stack exchange,提问作者Luis Felipe del Real Cintolesi
相关产品推荐
相关产品推荐

