javascript - 仅将根 URL 重定向到 Firebase 中的某个其他文件

标签 javascript firebase firebase-hosting

我正在为我的网络应用程序使用 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 中的内容。如何让根 URL 指向 lp.html?

最佳答案

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

Firebase Hosting only applies a rewrite rule if a file or directory does not exist at the specified source. When a rule is triggered, the browser returns the actual content of the specified destination file instead of an HTTP redirect.

您在 firebase.json 中指定的

redirects 有所不同。他们实际上会向用户的浏览器发送 301 或 302 重定向,使浏览器请求目标位置的 URL。不确定原始帖子实际上希望浏览器的 URL 看起来像什么,但这也可行:

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

关于javascript - 仅将根 URL 重定向到 Firebase 中的某个其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48910000/

相关文章:

javascript - Jquery函数获取文本并相应地改变div样式

javascript - 如何在 ExtJ 中深入分析图表

firebase - Firebase中基于多个where子句的查询

json - 从 Firebase 存储中获取图像

firebase - 如何在 Firebase 托管中关闭默认域 - firebase

python - 在 Firebase 托管中使用 Python Google Cloud Function

firebase - 将 Brotli 与 Firebase 托管结合使用

javascript - 如何将 apollo-link-http 与 apollo-upload-client 一起使用?

javascript - CoffeeScript 总是返回

android - 前台的 Firebase 通知