このチュートリアルでは、オリジンルールと DNS レコードを設定し、カスタムドメイン付きの Pages デプロイを指す方法を説明します。
手順では次の値を例に使います。
| 訪問者がアクセスする URL | mycustomerexample.com/blog/* |
| ゾーンのドメイン | mycustomerexample.com |
| Cloudflare Pages のサブドメイン | myblog.pages.dev |
| Cloudflare Pages のカスタムドメイン | blogmirror.example.com |
Pages のカスタムドメインを設定するときは、本番で使う予定のないカスタムドメイン(この例では blogmirror.example.com)を使います。
Pages デプロイへカスタムドメインを追加する手順は次のとおりです。
-
Cloudflare ダッシュボードで、Workers & Pages ページを開きます。
Workers & Pages を開く ↗ -
Pages プロジェクトを選択します。
-
Custom domains を開きます。
-
Set up a custom domain を選択します。
-
blogmirror.example.comを入力し、Continue を選択します。
Pages デプロイにカスタムドメインを追加すると、Cloudflare はそのカスタムドメイン向けの CNAME DNS レコードを自動作成します。
mycustomerexample.com ゾーンで、次の設定のオリジンルールを作成します。
受信リクエストが次に一致する場合
| フィールド | 演算子 | 値 |
|---|---|---|
| URI Path | wildcard | /blog/* |
Expression Editor を使う場合は、次の式を入力します。
(http.request.uri.path wildcard "/blog/*")オリジンパラメーターを設定する
- Host header > Rewrite to のあとの値:
blogmirror.example.com - DNS record > Override to のあとの値:
blogmirror.example.com
この例では、訪問者がアクセスする URL は /blog で始まります。一方、Pages デプロイにはこの先頭の URL セグメントはありません。
URL 書き換えを使い、URL パスから /blog セグメントを除きます。
-
Rules > Overview を開きます。
-
Create rule > URL Rewrite Rule を選択します。
-
Rule name にわかりやすいルール名を入力します。
-
If incoming requests match で Wildcard pattern を選択します。
-
Request URL に次の値を入力します。
https://<YOUR_HOSTNAME>/blog/*この例では、値は
https://mycustomerexample.com/blog/*になります。 -
Then rewrite the path and/or query の Path に、次の値を入力します。
Target path Rewrite to [ /]blog/*[ /]${1} -
Deploy を選択します。