这个选项仅在firebase.json
文件中设置一个标志,以将所有URL重定向到/index.html
。
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
"hosting": {
// ...
// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
// Serves index.html for requests to files or directories that do not exist
"source": "**",
"destination": "/index.html"
}, {
// Serves index.html for requests to both "/foo" and "/foo/**"
// Using "/foo/**" only matches paths like "/foo/xyz", but not "/foo"
"source": "/foo{,/**}",
"destination": "/index.html"
}, {
// Excludes specified pathways from rewrites
"source": "!/@(js|css)/**",
"destination": "/index.html"
} ]
}
**
。 - Oliver Dixon完整示例:
{
"hosting": {
"public": ".",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
如果您将其设置为“是”,则所有无效的URL(例如www.example.com/some-invalid-url
)都将被重定向到您网站的index.html
,这是一件好事。您也可以将其设置为自定义的404.html
。
firebase.json
{
"hosting": {
"public": "pubic",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"cleanUrls": true
}
}
奖励:将cleanUrls
设置为true
,以从已部署的网站URL中删除.html
扩展名,否则所有没有.html
的网址将重定向到index.html
。
rewrites
:"rewrites": [
{
"function": "angularUniversalFunction",
"source": "**"
}
]
去年我们使用了这个选项(Q1 & Q2),但似乎没有起作用,但现在当我们应用它时,事情确实变得非常不同。
它具体的官方解释可以在此查看:https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites
同样页面的下一节还有关于标头使用的一些有用信息。