将根URL重定向到Firebase中的其他文件



我正在为我的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服务并打开http://localhost:5000/时,我会看到index.html的内容。我如何将root URL指向lp.html?

将添加更多信息,因为所选答案不是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"
  }
]

出于某种原因,firebase默认情况下将index.html在根URL的情况下选择,而无需咨询firebase.json。为避免这种情况,我将index.html文件的名称更改为app.html,一切都按预期工作。

最新更新