如何通过Firebase托管将主页重定向到随机网址? [英] How can I redirect my homepage to a random url through firebase hosting?
问题描述
我正在尝试使用Firebase托管使用302重定向来重定向我的主页.
I am trying to redirect my homepage with 302 redirect using firebase hosting.
"redirects" :[{
"source": "/",
"destination": "what do i put here?",
"type": 302
}
]
在我的公用文件夹中,我有index.html,404.html和一个充满文件的文件夹. 我希望自己的主页随机重定向到这些文件之一.
In my public folder, I have the index.html, 404.html, and a folder full of files. I would like for my homepage to randomly redirect to one of those files.
我知道您可以使用location.href/assign/replace在index.html中进行重定向,但是我必须通过302重定向来实现.
I know you can redirect in the index.html with location.href/assign/replace, but I have to do it through 302 redirect.
谢谢.
推荐答案
一种可能的替代解决方案,涉及Cloud Functions:
A possible alternative solution, involving Cloud Functions:
在firebase.json中
In firebase.json
"hosting": {
"rewrites": [
{
"source": "**",
"function": "randomRedirect"
}
]
}
在函数的index.ts中(假设使用TypeScript):
In index.ts of your functions (assuming TypeScript):
export const randomRedirect = functions.https.onRequest((_, res: functions.Response) => {
res.redirect(302, 'your desired URL');
});
您需要删除您的index.html文件,以便在地址栏中更改URL.
You need to remove your index.html file for the URL to change in the address bar.
这篇关于如何通过Firebase托管将主页重定向到随机网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!