XSERVER で運用中のサイトで、サブドメインへリダイレクトしようとして、リダイレクトループにハマったのでメモ。
この記事の目次 非表示
前提 : XSERVER のディレクトリ構造
XSERVER でサブドメインを作成すると下記のような構造になり、ドキュメントルートにある .htaccess はサブドメインにも影響します。
example.com
└ public_html
├ .htaccess
└ sub.example.com(サブドメイン)
└ .htaccess
リダイレクトループにハマる
https://example.com/a.html の記事をサブドメイン https://sub.example.com/a.html に移動する場合、ドキュメントルートにある .htaccess に
Redirect permanent /a.html https://sub.example.com/a.html
などと書いてしまうと、サブドメインのルートにある a.html もリダイレクトしようとして、リダイレクトループとなります。
リダイレクトループにならない解決方法
この場合、ホスト名が https://example.com の時だけリダイレクトが発動するよう、下記のように記述すればうまくいきます。
RewriteEngine On
RewriteCond %{http_host} ^example.com
RewriteRule ^a.html https://sub.example.com/a.html [R=301,L]
以上です。お役に立てば幸いッ!