Search code examples
firebasefirebase-hosting

Dynamic rewrite rules with glob patterns in Firebase


I have a URL structure to a "project detail" page that looks like this: john/project/eRdKn6 (where "john" is the username and "eRdKn6" is the project id). I want this to be rewritten to the file project.html, where I parse the document.location and load the appropriate data. So the username and the project id should be dynamic.

The rule in firebase.json that I have looks like this:

{
  "source": "*/project/*",
  "destination": "/project.html"
}

However, Firebase 404's when I try to load http://example.com/john/project/eRdKn6.

I've tried to make only the last part dynamic, as a test (e.g. {"source": "john/project/*", "destination": "/project.html"} but I also get a 404.

The project.html is in the public folder.

Here is my full firebase.json file:

{
  "firebase": "example",
  "public": "public",
  "ignore": [
    "firebase.json",
    "**/.*",
    "**/node_modules/**"
  ],
  "rewrites": [
    {"source": "*/project/*", "destination": "/project.html"}
  ]
}

Solution

  • It seems the issue is that source requires a starting slash, e.g. /*/project/*. My full firebase.json now looks like this:

    {
      "firebase": "example",
      "public": "public",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {"source": "/*/project/*", "destination": "/project.html"}
      ]
    }
    

    This seems a documentation issue, since none of the examples cover this use case.