Firebase hosting redirect to another subdomain keeping query and params

Viewed 44

How can I redirect calls to another subdomain, keeping all the request as the original using Firebase hosting. I could do it at web application level but this will lead to performances issues.

Ex :

https://some.app.com/route/param?=something&others

to

https://different.app.com/route/param?=something&others

1 Answers

I was working on something similar, here is my current attempt as a firebase node function:

exports.redirectExample = functions.https.onRequest((req, res) => {
  const redirectDomain = "example.org";
  const subdomains = req.subdomains;
  if (subdomains) {
    const reverseJoinSubdomains = subdomains.reverse().join('.');
    res.redirect("https://" + reverseJoinSubdomains + "." + redirectDomain + req.path);
  } else {
    res.redirect("https://" + redirectDomain + req.path);
  }
});

I could not get this to work, when I logged req.subdomains it was empty even though I have pointed multiple A Record subdomains to this function. Perhaps subdomain on request object is lost due to the way these servers are setup, not sure. I'll revert to simply forward to a static hostname regardless of subdomain, but if anyone has an answer to why req.subdomains doesn't work in firebase functions please let us know!

Related