Search code examples
firebasefirebase-hostingreroute

Firebase Hosting Route ReWrites (Abnormal Paths)


I'm trying to create a firebase routing rule that will re-route any request under a specific sub-directory to one particular file on the server.

This works if I have a normal filepath (with an extension), but does not seem to work if I have removed the extension from the initial request.

Is anyone aware of how this sort of 'rewrite' logic works and is there a way to leverage in this manner?

(or am i just doing this wrong, since it's not clear to me why the first rule doesn't work either)

Using this rule-set:

    "rewrites": [
      {
        "source": "/access-token/somefolder/else.html",
        "destination": "/access-token/2.json"
      },
      {
        "regex": "^/access-token/[\\d]+$",
        "destination": "/access-token/2.json"
      },
      {
        "regex": "^/access-token/[\\d]+\\.json$",
        "destination": "/access-token/1.json"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]

Results of testing:

request : https://[root]//access-token/somefolder/else.html   <-- this path does not exist, i was only using this as a test
expected: routes to 'destination'
actual  : routes to root (probably hitting final rule?)


request : https://[root]/access-token/12
expected: routes to 'destination'
actual  : routes to "404 not found"


request : https://[root]/access-token/12.json
expected: routes to 'destination'
actual  : re-routes as intended


Solution

  • For the first issue, as redirects take a higher priority than rewrites, it is possible that the .html has already been stripped from the incoming URL by the time it hits the rewrite engine.

    {
      "source": "/access-token/somefolder/else",
      "destination": "/access-token/2.json"
    }
    

    For the following items, don't escape the \ character and you don't need the anchors either IIRC.

    {
      "regex": "/access-token/\d+",
      "destination": "/access-token/2.json"
    }
    
    {
      "regex": "/access-token/\d+\.json",
      "destination": "/access-token/1.json"
    }