You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase部署React应用仅显示首页,其他页面404问题求助

解决React单页应用部署Firebase后子页面404的问题

这是因为Firebase Hosting默认会按请求路径查找对应的静态文件,但React单页应用用的是前端路由,所有路由逻辑都在index.html里处理,直接访问子路径时Firebase找不到对应文件,就返回404了。

解决方法很简单,只需要在firebase.json里配置路由重写规则:

  1. 打开项目根目录的firebase.json(没有的话直接创建)
  2. 在hosting配置块中添加rewrites规则,把所有请求都导向index.html

示例配置:

{
  "hosting": {
    "public": "build", // 注意:这里填你的React构建产物目录,默认是build
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

配置完之后,重新部署到Firebase:

firebase deploy --only hosting

这样再访问站点内的子页面,Firebase就会返回index.html,由React的路由系统来渲染对应的页面了。

内容的提问来源于stack exchange,提问作者Peter Bonnebaigt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 00:36:27