如何在Firebase中删除.html URL结尾并配置重定向?

How to remove .html URL endings and configure redirects in Firebase?

提问人:ttoshiro 提问时间:6/15/2020 最后编辑:ttoshiro 更新时间:6/24/2020 访问量:3472

问:

编辑:修复了一个错误,我在HTML中引用了该错误,但仍然遇到相同的问题。about.html/about/


我知道这是以前被问过很多次的问题的重复,但其他人的解决方案对我不起作用。我试图做的是重新路由,例如,到 和 到 .index.html/index/about.html/about/

我尝试在这里使用 @louisvno 的答案,在这里使用 @michael-bleigh 的答案,在这里使用 @HarsH 的答案,所有这些都是为了将以下内容添加到 .json 文件中:

"hosting": {
    "cleanUrls": true
  }

(这也是Firebase配置托管行为指南的“控制.html扩展”部分的答案。)

但是,这没有效果。在我的 HTML 文件中,我尝试将标签设置为 、 和 ,但这些选项都不起作用,我不确定是否有必要从原始更改它。(是吗?<a>href="index.htmlhref="/index/href="/index"<a href="index.html">

我尝试的另一个解决方案是利用该属性,正如 @ken-mueller 在这里建议的那样。我删除了“clearUrls”位,并将以下内容放在我的.json文件中:"redirects"

  "hosting": {
    "redirects": [ {
      "source": "index.html",
      "destination": "/index",
      "type": 301
    }, {
      "source": "index.html{,/**}",
      "destination": "/index",
      "type": 3011
    }, {
      "source": "about.html",
      "destination": "/calculator",
      "type": 301
    }, {
      "source": "about.html{,/**}",
      "destination": "/solutions",
      "type": 301
    } ]
  }

(这也是 Firebase 配置托管行为指南的“配置重定向”部分的答案)

这也没有任何效果。而且,即使将我的 HTML 从 更改为 或 ,它也只是破坏了页面并显示页面。<a href="about.html"><a href="/about/"><a href="/about">about.htmlindex.html

我做错了什么?如何从 URL 末尾删除 .html 并确保在 URL 中输入任何时候它都会重定向到没有结尾的 URL(即 而不是?/index//index.html

firebase 网址 重定向 firebase-hosting

评论

1赞 Frank van Puffelen 6/15/2020
在 firebase.json 文件中进行设置允许您将文件关闭,因此成为 (无尾随 )。如果这对您不起作用,您能否向我们指出您配置此 URL 的 URL?"cleanUrls": true.html/index.html/index/
0赞 ttoshiro 6/15/2020
感谢您的回复!我在 carbonfootprint.dev 上主持这个。由于某种原因仍然无法正常工作。
1赞 Frank van Puffelen 6/15/2020
干净的 URL 对我有用,例如:carbonfootprint.dev/calculator。但是从阅读文档来看,托管似乎也应该自动重写为 ,这似乎没有发生。您可能需要联系 Firebase 支持团队,以获得有关问题排查的个性化帮助。https://carbonfootprint.dev/calculator.htmlhttps://carbonfootprint.dev/calculator
0赞 ttoshiro 6/16/2020
实际上链接到错误页面的干净 URL 应该链接到,但由于某种原因它显示索引。这有什么意义吗?/calculatorcalculator.html
1赞 Frank van Puffelen 6/16/2020
嗯......这听起来像是您有一个重写,覆盖了干净的 URL 重定向。我实际上不确定这些标志的组合应该如何工作。

答:

12赞 ttoshiro 6/24/2020 #1

最终解决了这个问题,这要归功于 Firebase 支持的 Mau。 工作正常,我只需要一个开括号和闭合括号(呃)。对于我帖子中的代码,我还注意到其中一个重定向已设置为而不是 .无论哪种方式,最终代码都是:"cleanUrls""type":3011301

{
  "hosting": {
    "cleanUrls": true,
    "trailingSlash": true
  }
}