【问题标题】:How to redirect 404 to start page?如何将 404 重定向到起始页?
【发布时间】:2018-07-13 12:30:24
【问题描述】:

我刚开始在 firebase 上托管一个 Web 应用程序。 使用firebase cli (firebase-tools) 的自动设置在我的公共文件夹中创建了一个404.html 文件。

但我不想看到自定义的 404 错误页面,也不想看到来自 firebase 的默认页面。 我想将所有 404 重定向到起始页,以便应用程序无论如何都会被初始化。 (我使用webpack-dev-server 的本地设置工作正常)

解决方法是将index.html 中的相同内容放入404.html。但这会导致维护翻倍..

我在这里找到了一些关于重定向配置的信息https://firebase.google.com/docs/hosting/url-redirects-rewrites。 但无法重定向 404 错误类型。 无法使用.htaccess 文件。

我是否错过了更改此行为的正确位置,无论是在我的 firebase.json 文件还是在 https://console.firebase.google.com/ 中??

【问题讨论】:

    标签: firebase http-status-code-404 firebase-hosting


    【解决方案1】:

    不确定这是否适合你,我从来没有用 .htaccess 做过任何事情,但就我而言,我总是这样修复它:

    您可以在 firebase.json 文件的 rewrites 部分中包含通配符路由作为最后一个路由,因此任何以前无人认领的路由都将匹配:

    "rewrites": [
      {
        // route info here
      },
      {
        // another route
      },
      {
        // If it makes it here, it didn't match any previous routing
        // Match all routes that get to this point and send them to the home page.
        "source": "**",
        "destination": "/index.html"
      }
    ]
    

    【讨论】:

    【解决方案2】:

    如果您不想留下原始/不存在的 url(例如https://example.com/asdf),您可以修改 404.html 源并将其替换为 js 重定向。

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Page Not Found</title>
        <script>
          window.location.href = "/";
        </script>
      </head>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2021-11-03
      • 1970-01-01
      • 2011-07-02
      • 2018-03-11
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多