简体   繁体   中英

How to Configure hosting behavior in firebase.json to rewrite URL?

I am developing a web app and hosted all files on Firebase Hosting.

I want when someone type this URL

mydomain.com/SomeUsername   //SomeUsername is URL Variable

it should open this URL

mydomain.com/user/index.html?user=SomeUsername   //SomeUsername is URL Variable

I tried this but not working, Its redirecting to 404 Page Not Found.

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [ {
    "source": "/user/index.html?user=:SomeUsername*", 
    "destination": "mydomain.com/:SomeUsername*",
    "type": 301
  }]
  }
}

Try removing the "type" from the rewrite as this is a rewrite, not a redirect. Also, try removing the domain name from the destination by changing it from:

"mydomain.com/:SomeUsername*"

to

"/:SomeUsername*"

The code should say:

 "rewrites": [ {
    "source": "/user/index.html?user=:SomeUsername*", 
    "destination": "/:SomeUsername*",

I found other solution, When I type mydomain.com/SomeUsername It will redirect me to 404.html which is obvious this page is not available. I edited 404.html to redirect to actual page.

404.html

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script>
      const currentUrl = window.location.href;
      var a = document.createElement('a')
      a.setAttribute('href',currentUrl)
      var uname = a.pathname.replace('/','')
      window.location = "https://yourdomain/user/index.html?username="+uname;
    </script>
  </body>
</html>

and in user/index.html page

if(history.replaceState) history.replaceState({}, "", "/username"); 

just to show url look like this mydomain.com/username

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM