LoginSignup
1
0

More than 3 years have passed since last update.

Firebase Hosting+Cloud Functionsでリクエストのドメインを取る方法

Last updated at Posted at 2020-11-30

問題設定

例えば、Firebase Hostingでこれらのドメインをホストしていたとしよう。
一番上が自分で用意したドメインで、下二つはFirebaseがくれたドメインだ。

  • example.com
  • example.web.app
  • example.firebaseapp.com

example.web.appをexample.comにリダイレクトしたいことがある。
これをCloud Functionsで実装することにする。

ところが、Cloud Functionsで受け取るHttp RequestオブジェクトはhostがCloud Functionsのホストになっている。
なので、if (req.header("host") === "example.web.app") ... などと書いてもダメなのだ。

結論

x-forwarded-hostを使おう。

functions.ts
  if (req.header("x-forwarded-host") === "example.web.app") {
    res.redirect(301, "https://example.com/");
    return;
  }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0