Firebase:在 URL 重写中传递 url 参数

Firebase: Pass url Param in URL Rewrite

对于 firebase 文档中的代码示例,它说要像这样启动 url 重写:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "**",
            "destination": "/index.html"
          } ]
        }

索引页要传参数怎么办?我试过了:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "/item/**",
            "destination": "/item.html?i="
          } ]
        }

但这并没有做任何事情..

我也试过下面的答案:

 "hosting": {
  // Add the "rewrites" section within "hosting"
  "rewrites": [ {
    "source": "/item/:item",
    "destination": "/item.html?i=:item"
  } ]
}

但这只是 returns 一个 404 页面。

    "hosting": {
      // Add the "rewrites" section within "hosting"
      "rewrites": [ {
        "source": "/item/:item",
        "destination": "/item.html?i=:item"
      } ]
    }

试一试。

FireBase Allows for Static web hosting. It also has some Custom Behaviours

 https://firebase.google.com/docs/hosting/url-redirects-rewrites

Redirects:: this is only for page forwarding 
Rewrites:: when you want to show the same content for multiple URLs [ it is there for URL reversing ].
It also provides control over the MIME types of the HTTP requests.

现在,根据上面的问题,我看到您正在尝试从动态网络主机提供的页面中查询 DOM 对象。

https://www.quora.com/How-are-dynamic-websites-hosted-1

我刚刚收到一封来自 Firebase 支持的电子邮件,内容如下:

Update From Firebase support:

Your use case is not possible with Firebase Hosting alone. You will need to make use of Cloud Functions as well to do so. Firebase has just recently released a native Firebase Hosting + Functions integration which makes it possible to perform server-side processing so you can redirect URLs to a function where you can write code to dissect the path and generate whatever response you want. You may check our documentations out to know more about it in detail.

https://firebase.google.com/docs/hosting/functions

我已经给他们回了邮件,看看这是否会在未来添加,因为它似乎对一页的 运行 进程有点矫枉过正。

2017 年 7 月 28 日更新

Since this is not supported, I have filed a feature request for you. However, I am not able to share any details or timelines for now. We'll keep your feedback in consideration moving forward though.

In the meantime, you may keep an eye out on our release notes.

Have a great day.

https://firebase.google.com/support/releases

我知道这是一个老问题,但我遇到了类似的问题但没有找到答案所以我想分享我是如何解决它的。

{    
    "hosting": {
    // Add the "rewrites" section within "hosting"
        "rewrites": [ {
            "source": "/item/**",
            "destination": "/item.html"
        } ]
}

我通过将动态 URL 重写为静态 html 页面解决了我的问题。由于它是重写,因此 URL 将保持 source URL 模式,不会像重定向那样更改为 destination URL。

然后我们可以在 item.html 页面上阅读 source URL 到 window.location.pathname 和 Javascript。然后,您可以使用 .split('/') 到 return 一个数组来选择您需要的部分。

希望这对寻找类似解决方案的任何人有所帮助。

我 运行 也遇到了同样的问题。虽然重写是不可能的,但我发现 redirects:

是可能的
"redirects": [
  {"source": "/user/friendly/url.html",
   "destination": "/userunfriendly.html?myid=42",
   "type": 301
  },