0

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

LeftBrain
  • 326
  • 2
  • 10

1 Answers1

0

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"
}
samthecodingman
  • 23,122
  • 4
  • 30
  • 54
  • this could just be linting in my IDE, but without the escapes, i'm getting errors: ``` Invalid escape character in string.json(261) ``` – LeftBrain Sep 12 '22 at 16:33
  • Oddly enough, removing the extension (.html) from the first rule made no difference. removing the anchors in the others seems to have provided what I'm looking for, thx for the help. – LeftBrain Sep 12 '22 at 16:51