【问题标题】:Redirect only the root URL to some other file in Firebase仅将根 URL 重定向到 Firebase 中的其他文件
【发布时间】:2025-12-13 19:45:02
【问题描述】:

我正在为我的 Web 应用程序使用 Firebase 托管。

我想在我的 URL 的根目录显示 lp.html 并在其他任何地方显示 index.html。为了实现这一点,我已经像这样配置了我的 firebase.json:

{
  "hosting": {
    "public": "build/es6-bundled",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/",
        "destination": "/lp.html"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

但是当我使用 firebase serve 并打开 http://localhost:5000/ 时,我看到了 index.html 中的内容。如何让根 URL 指向 lp.html?

【问题讨论】:

    标签: javascript firebase firebase-hosting


    【解决方案1】:

    将添加更多信息,因为所选答案并非 100% 正确。首先,澄清rewritesredirects 之间的区别很重要。在 firebase.json 中指定 rewrites 后,如果没有首先找到指定的文件,Firebase 确实只会根据您的重写规则执行操作。这是文档中的相关部分:

    Firebase 托管仅在指定源中不存在文件或目录时应用重写规则。触发规则时,浏览器会返回指定目标文件的实际内容,而不是 HTTP 重定向。

    您在 firebase.json 中指定的redirects 有所不同。他们实际上会向用户的浏览器发送 301 或 302 重定向,以使浏览器在目标位置请求 URL。不确定原始帖子实际上希望浏览器的 URL 是什么样子,但这也可以:

    "redirects":[ 
      {
        "source": "/",
        "destination": "/lp.html",
        "type": 301
      }
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
    

    【讨论】:

      【解决方案2】:

      结果出于某种原因,firebase 默认选择在根 URL 处提供 index.html,而无需咨询 firebase.json。为避免这种情况,我将 index.html 文件的名称更改为 app.html,一切正常。

      【讨论】:

      • 我遇到了同样的问题。感谢您的解决方案,它也对我有用。
      • 也为我工作。这是否记录在 Firebase 中的任何地方?
      • @IndikaK 我在文档中的任何地方都没有找到这个。我只是根据常识发现了这一点,哈哈
      • 这不是真的。您可以在firebase.json 中使用redirects
      最近更新 更多