firebase.json 使用通配符重定向无效

firebase.json redirects with wildcard not working

我正在尝试将呼叫 mywebsite.com/poly/1 的人重定向到相应的 .png 图片,但它不起作用。

我试过使用 Glob:

    "rewrites": [
      {
        "source": "/poly/:tokenId",
        "destination": "/images/:tokenId/img.png",
        "type": 301,
        "headers": [ {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        }]
      }
    ]

正则表达式:

    "rewrites": [
      {
        "regex": "/poly/(\d+)",
        "destination": "/images/:1/img.png",
        "type": 301,
        "headers": [ {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        }]
      }
    ]

我总是收到 404,但如果目的地是静态的,它会起作用,例如: "destination": "/images/1/img.png",

我已经使用 firebase serve 进行了测试。

有什么想法吗?

您正在混合 rewrites and redirects 的语法。

// rewrites
"hosting": {
  "rewrites": [ {
    "source": "**",
    "destination": "/index.html"
  } ]
}
// redirects
"hosting": {
  "redirects": [ {
    "source": "/foo",
    "destination": "/bar",
    "type": 301
  } ]
}

重写匹配源文件并提供目标文件。他们不支持 URL 段。

重定向匹配源并告诉浏览器改用目标路径。他们确实支持 URL 个细分市场。

如果您不介意目标路径可用,您可以使用重写。您可以将文件上传到 Firebase 存储并通过函数代理文件,但这并不理想。