Search code examples
firebaseflutterflutter-webfirebase-hosting

Firebase keeps redirecting example.com/username to example.com/#/


I am trying to extract the username from the current URL example.com/username but firebase keeps directing it to example.com/#/ due to which username changes dynamically.

The website is built on flutter

firebase.json file:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "hosting": {
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/**",
        "dynamicLinks": true,
        "destination": "/index.html"
      }
    ]
  },
  "storage": {
    "rules": "storage.rules"
  }
}

Visuals:

Visuals of redirection


Solution

  • If we look at the rewrites node in your firebase.json:

    "rewrites": [
      {
        "source": "/**",
        "dynamicLinks": true,
        "destination": "/index.html"
      }
    ]
    

    This tells Firebase Hosting to rewrite any incoming path ("source": "/**") to the same index.html file ("destination": "/index.html").

    If you don't want it to perform this rewrite, you should remove it from the firebase.json configuration file.