LoginSignup
2
2

More than 5 years have passed since last update.

新ドメインへパスごとリダイレクトする

Last updated at Posted at 2012-08-14

WEBサイトを運営しているとドメインが変えなくてはならないときがあったりします。

コンテンツはパスを含めて全部移行しても、古いURLでアクセスしてきたユーザをきちんと誘導することをしなければ片手落ちです。なので簡単にその方法をまとめてみます。

一般的な方法

一般的には、旧サーバの.htaccessで次のような設定をします。

RedirectMatch permanent (.*) http://newserver.com$1

PHPを使った方法

何らかの理由で上の RedirectMatchが使えないときは、下のような方法も取ることができます。

旧サーバでmod_rewirteで index.php を動かして、REQUEST_URI に応じてリダイレクトする方法です。

.htaccessはこんなかんじです。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [QSA,L]

phpはこんなかんじです。headerで3つ目の引数にHTTPステータス 301(Moved Permanently)を指定しています。

index.php
<?php
$url = 'http://newserver.com' . $_SERVER['REQUEST_URI'];
header('Location:' . $url, true, 301);

phpで書いておけばこれ以外の処理が必要だったりした時も簡単に拡張できるのでもしかしたら結構いいかもしれません。

2
2
2

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
  3. You can use dark theme
What you can do with signing up
2
2