問題設定
例えば、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;
}