Search code examples
firebasefirebase-hosting

firebase.json redirects with wildcard not working


I am trying to redirect people that call mywebsite.com/poly/1 to a corresponding .png image but it is not working.

I have tried using Glob:

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

Regex:

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

I always get a 404, but it works if the destination is static like: "destination": "/images/1/img.png",

I have tested this using firebase serve.

Any ideas?


Solution

  • You are mixing the syntax of rewrites and redirects.

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

    Rewrites match a source and serve destination files. They don't support URL segments.

    Redirects match a source and tell the browser to use the destination path instead. They do support URL segments.

    You could use a rewrite if you don't mind the destination paths being available. You could upload the files to Firebase Storage and proxy the files through a Function but that would not be ideal.