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

tto*_*iro 6 url redirect firebase firebase-hosting

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


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

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

"hosting": {
    "cleanUrls": true
  }
Run Code Online (Sandbox Code Playgroud)

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

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

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

  "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
    } ]
  }
Run Code Online (Sandbox Code Playgroud)

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

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

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

tto*_*iro 12

感谢 Firebase 支持部门的 Mau,最终解决了这个问题。"cleanUrls"工作正常,我只需要一个左括号和右括号(废话)。对于我帖子中的代码,我还注意到其中一个重定向已"type":设置3011为而不是301. 不管怎样,最终的代码是:

{
  "hosting": {
    "cleanUrls": true,
    "trailingSlash": true
  }
}
Run Code Online (Sandbox Code Playgroud)