LoginSignup
0

More than 1 year has passed since last update.

Netlifyで新旧の独自ドメインでリダイレクトする設定5行

Last updated at Posted at 2021-02-18

この投稿ではNetlifyで独自ドメインをリダイレクトする設定方法を説明します。

例えば、old.example.comをnew.example.comにリダイレクトする設定をする場合、netlify.tomlに次の[[redirect]]セクションを追加するだけです。

netlify.toml
[[redirects]]
  from = "https://old.example.com/*"
  to = "https://new.example.com/:splat"
  status = 301
  force = true

この設定にすると、old.example.comのトップページはもちろん、old.example.com/posts/1のようなパスもリダイレクトされます。

  • https://old.example.com/https://new.example.com/
  • https://old.example.com/posts/1https://new.example.com/posts/1

forceはfalseにすると、ファイルがある場合はリダイレクトしない設定になります。例えば、ファイル/aboutme.htmlが有るとき、old.example.com/aboutme.htmlへのアクセスはnew.example.com/aboutme.htmlへのリダイレクトされず、普通にそのファイルの中身が200ステータスで帰ってきます。ドメインのリダイレクト設定はなんのためにやるかというと、ドメイン名を変更するケースで行うものが大半だと思うので、ファイルが有るときだけはリダイレクトしないといった使い方はあまり考えられません。なので、forceはtrueにしておきます。

上の設定では、httpsにしか触れてませんが、httpsではなくhttpでアクセスした場合はどうなるかというと、

http://old.example.com/page
↓
https://old.example.com/page
↓
https://new.example.com/page

という間にhttp://oldからhttps://oldへのリダイレクトをはさむ流れで最終的に新ドメインにリダイレクトされます。

これは無駄だな、http://oldからhttps://newに直接リダイレクトしたいなという場合は、httpとhttpsの両方の設定をしてあげると、リダイレクトは1回だけで済むようにもできます:

netlify.toml
[[redirects]]
  from = "http://old.example.com/*"
  to = "https://new.example.com/:splat"
  status = 301
  force = true

[[redirects]]
  from = "https://old.example.com/*"
  to = "https://new.example.com/:splat"
  status = 301
  force = true

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
What you can do with signing up
0