ドメイン変更時の新ドメインにリダイレクト [mod_rewrite]

2021-06-21

ドメイン変更を行った際に、旧ドメインにアクセスがあった場合に新ドメインに転送する設定を紹介します
説明の際に旧ドメイン:old.hoge.com 新ドメイン:new.hoge.com として説明します

新しいドメインに転送

例)old.hoge.com/ → new.hoge.com/
例)old.hoge.com/xxx.html → new.hoge.com/

旧ドメイン:old.hoge.com 新ドメイン:new.hoge.comに転送します。
ただし、old.hoge.com/xxx.htmlなどのURLにあった場合にも、新ドメイン:new.hoge.comのトップページにアクセスします

RewriteEngine On
RewriteRule / http://new.hoge.com

旧ドメインにアクセスを新しいドメインに転送

例)old.hoge.com/ → new.hoge.com/
例)old.hoge.com/xxx.html → new.hoge.com/xxx.html
例)old.hoge.com/abc/xxx.html → new.hoge.com/abc/xxx.html

旧ドメイン:old.hoge.com/abc/xxx.html にアクセスのあった場合、新ドメイン:new.hoge.comabc/xxx.htmlに転送します。ドメインの後の「abc/xxx.html」を保持したまま新ドメインに転送します

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.hoge\.com [NC]
RewriteRule ^(.*)$ http://new.hoge.com/$1 [R=301,L]

wwwのない場合、wwwのある方に転送

例)hoge.com → www.hoge.com
http://hoge.com のようにwwwのURLアクセスがあった場合に、www.hoge.com に 転送(リダイレクト)する方法

RewriteEngine On
RewriteCond %{HTTP_HOST} ^hoge\.com [NC]
RewriteRule ^(.*)$ http://www.hoge.com/$1 [R=301,L]

スポンサーリンク

0
0