- Published on
Firebase Hostingでの301リダイレクトの仕方
Firebase Hostingでは、redirectsやrewriteが使えます。 サイト移行の際や、URLのリライトにも使えます。
今回は、サイト移行を想定した301のリダイレクトの方法を紹介します。
実はとても簡単で、firebase.jsonに下記のようにredirectsを追加するだけです。
{ "hosting": { "site": "....", "public": "public", "ignore": ["firebase.json", "**/.*", "**/node_modules/**"], "redirects": [ { "source": "/foo", "destination": "https://example/foo", "type": 301 } ] } }
上記のようにすることで、/fooというアクセスをhttps://example.com/fooに301でリダイレクトできるようになります。
また、セグメントといって、可変的な文字列を捉えるやり方や、正規表現を使って、リダイレクトの処理も可能です。
{ "hosting": { "site": "....", "public": "public", "ignore": ["firebase.json", "**/.*", "**/node_modules/**"], "redirects": [ { "source": "/foo/:page", "destination": "https://example/foo/:page", "type": 301 } ] } }
:pageとする部分で例えば、/foo/hoge-hogeのようなリクエストをhttps://example.com/foo/hoge-hogeにリダイレクトできます。正規表現の場合は下記のようになります。
{ "hosting": { "site": "....", "public": "public", "ignore": ["firebase.json", "**/.*", "**/node_modules/**"], "redirects": [ { "regex": "/foo/(?P<page>.*)", "destination": "https://example/foo/:page", "type": 301 } ] } }