我正在为我的网络应用程序使用 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% 正确。首先,澄清rewrites
与redirects
之间的区别很重要。通过在 firebase.json 中指定 rewrites
,如果没有首先找到指定的文件,Firebase 确实只会根据您的重写规则执行操作。这是文档中的相关部分:
您在 firebase.json 中指定的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.
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/