![](/img/trans.png)
[英]How to Configure hosting behavior in firebase.json to rewrite 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=$1"
} ]
}
但这没有做任何事情..
我也试过下面的答案:
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/:item",
"destination": "/item.html?i=:item"
} ]
}
但这只会返回 404 页面。
我知道这是一个老问题,但我遇到了类似的问题并且没有找到答案所以我想分享一下我是如何解决它的。
{
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/**",
"destination": "/item.html"
} ]
}
我通过将动态 URL 重写为静态html
页面解决了我的问题。 由于它是重写,因此 URL 将保留为source
URL 模式,不会像重定向那样更改为destination
URL。
然后,我们可以在item.html
页面上使用 Javascript 通过window.location.pathname
读取source
URL。 然后,您可以使用.split('/')
返回一个数组来选择您需要的部分。
希望这有助于任何寻找类似解决方案的人。
我刚刚收到一封来自 Firebase 支持的电子邮件,内容如下:
从 Firebase 支持更新:
单独使用 Firebase 托管无法实现您的用例。 为此,您还需要使用 Cloud Functions。 Firebase 最近刚刚发布了原生的 Firebase Hosting + Functions 集成,这使得执行服务器端处理成为可能,因此您可以将 URL 重定向到一个函数,您可以在其中编写代码来剖析路径并生成您想要的任何响应。 您可以查看我们的文档以详细了解它。
我已经给他们回了邮件,看看这是否会在未来添加,因为运行一页的进程似乎有点矫枉过正。
由于不受支持,我已为您提交了功能请求。 但是,我现在无法分享任何细节或时间表。 不过,我们会继续考虑您的反馈。
同时,您可以留意我们的发行说明。
祝你有美好的一天。
我也遇到了同样的问题。 虽然重写是不可能的,但我发现 重定向是可能的:
"redirects": [
{"source": "/user/friendly/url.html",
"destination": "/userunfriendly.html?myid=42",
"type": 301
},
"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.
现在,从上面的问题中我看到您正在尝试从动态 Web 主机提供的页面中查询 DOM 对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.