【问题标题】:How can I redirect my homepage to a random url through firebase hosting?如何通过firebase托管将我的主页重定向到随机网址?
【发布时间】:2019-10-21 03:52:08
【问题描述】:

我正在尝试使用 Firebase 托管通过 302 重定向重定向我的主页。

"redirects" :[{
    "source": "/",
    "destination": "what do i put here?",
    "type": 302

    }
]

在我的公用文件夹中,我有 index.html、404.html 和一个装满文件的文件夹。 我希望我的主页随机重定向到其中一个文件。

我知道你可以在 index.html 中使用 location.href/assign/replace 进行重定向,但我必须通过 302 重定向来完成。

谢谢。

【问题讨论】:

    标签: json firebase firebase-hosting


    【解决方案1】:

    可能的替代解决方案,涉及 Cloud Functions:

    在 firebase.json 中

    "hosting": {
      "rewrites": [
        {
          "source": "**",
          "function": "randomRedirect"
        }
      ]
    }
    

    在你的函数的 index.ts 中(假设是 TypeScript):

    export const randomRedirect = functions.https.onRequest((_, res: functions.Response) => {
        res.redirect(302, 'your desired URL');
    });
    

    编辑:您需要删除您的 index.html 文件,以便在地址栏中更改 URL。

    【讨论】:

    • 感谢您的解决方案。我测试了它,我忘了提到这一点,但是有没有办法在每次刷新页面时获取不同的随机 url?代码重定向到一个随机 url,但在刷新时不会改变。
    • 我明白了。也许它与缓存有关。我不确定,我从来没有用 302 这样做过,但你可以尝试在 res.redirect 之前添加 res.set('Cache-Control', 'public, max-age=0, s-maxage=0');
    • 也可以尝试改写成"function": "randomRedirect?random=please",重新部署函数和托管。
    • 抱歉,您上面的代码在显示这些网址的内容方面是正确的。 (我在导出函数外部的变量中调用了随机函数)但是我需要使用重定向更改 url。这没有发生,因为我假设它是托管的重写属性中的一个功能。有什么想法吗?
    • 哦,是的,对不起,我留下了 index.html,其中可能保持 url 不变。感谢您的所有帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 2018-07-30
    • 2016-04-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多