firebase hostingで301リダイレクト設定を行う

公開日時

先日、このブログを

Gridsome + Contentful + Firebase Hosting

から

Next.js + Contentful + Vercel

に移行した。

また、その少し前にブログのドメインも変更した。

久しぶりに旧ドメインの方のSearch Consoleを眺めていたところ、現在も検索結果に旧ドメインが表示されている記事がいくつかあったのでfirebase hosting設定で新ドメインに301リダイレクトさせることにした。

一部のpathはNext.js移行時に変更したが、Next.jsで動的URLをRewriteすることで以前のpathでもアクセスできるようにしておいたので、firebase hostingでは全pathを新ブログのURLにリダイレクトするだけで済んだ。

firebase.jsonhosting.redirects に以下を設定した。

{
  "hosting": {
    "public": "dist",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "redirects": [
      {
        "source": "/:path*",
        "destination": "https://sunday-morning.app/:path",
        "type": 301
      },
      {
        "source": "/",
        "destination": "https://sunday-morning.app/",
        "type": 301
      }
    ]
  }
}

これで旧ドメインの記事にアクセスしても新ドメインの記事が表示されるようになった。

参考


Related #firebase

Firestore エミュレータ使用時にデータをリセットする

エミュレータの場合のみ一括削除用のAPIが用意されている

Error: Input required and not supplied: firebaseServiceAccount

Dependabotによって作成されたPull Requestのみで発生している

Firebase公式Github ActionsでPull RequestごとにHostingのプレビュー環境を作る

手軽にプレビュー環境で動作確認できるようになったので、継続的なアップデートがやりやすくなった