ページが移動(引越し)してURLが変更したことを示すステータスコード301をhttpd.conf(apache.conf)に記す方法を以下に記します。
レンタルサーバなどを使用している場合は、.htaccessに記述すれば同様の動作となります。
ページが移動(引越し)しURLを変更した場合、サーチエンジンにも反映されるようにするには、
HTTPのステータスコード301を送ることによりサーチエンジンにページが移動したことを伝えることができます。
また、サーチエンジンで検索し移動前のリンクをクリックしてもブラウザにステータスコード301が送信されるため、
ブラウザも新しい引越先のページを開くようになります。
以下の構文でステータスコード301でリダイレクトするようになります。
Redirect permanent ディレクトリ http://ドメイン/ディレクトリ/上記の例として以下のようになります。
Redirect permanent /old/ http://web.just4fun.biz/new//old/にあるコンテンツは全てweb.just4fun.bizのnewディレクトリに移動したことを意味します。
Redirect permanent ファイル http://ドメイン/ディレクトリ/ファイル上記の例として以下のようになります。
Redirect permanent /old/page.html http://web.just4fun.biz/new/page.html/old/page.htmlはてweb.just4fun.bizのnew/page.htmlに移動したことを意味します。
CentOS6を使用して実際に設定してみます。
(他のLinuxディストリビューションやWindowsでは設定ファイル名や操作方法が異なる場合がありますので読みかえてください。)
サーバー名はcentos6になっています。
CentOSでは、/etc/httpd/conf/httpd.confを直接修正しなくてもhttpd/conf.d/に設定ファイルを作成し保存すれば、作成したファイルを読み込んでくれるように/etc/httpd/conf/httpd.confに設定されています。(以下参照)
# # Load config files from the config directory "/etc/httpd/conf.d". # Include conf.d/*.conf
したがって、本記事ではhttpd/conf.d/にredirect301.confというファイル名で設定ファイルを作成し保存しました。
以下に簡単な例を記します。
Redirect permanent /old.html http://centos6/new.html
# service httpd restart
複数のページを指定する場合は以下のように下に追記すれば実現できます。
# cat /etc/httpd/conf.d/redirect301.conf Redirect permanent /old.html http://centos6/new.html Redirect permanent /old2.html http://centos6/new2.html
以下のようなディレクトリ構成でテストしてみます。
treeコマンドはrootユーザで yum install tree をすればインストールすることができます。
# pwd /var/www/html # tree old new old ├── 1.html └── 2.html new ├── 1.html └── 2.html
# cat /etc/httpd/conf.d/redirect301.conf Redirect permanent /old/ http://centos6/new/
# service httpd restart
上記では、httpd.conf(apache.conf)を修正してステータスコード301のリダイレクトを実現していました。
以下は、httpd.conf(apache.conf)を修正することができない場合(レンタルサーバー等)の対処方法です。
ファイル名.htaccessを使用して実現してみます。
設定に関しては上記のhttpd.conf修正と同じにしました。
.htaccessファイルが存在しない場合は作成してください。
Redirect permanent /old.html http://centos6/new.html Redirect permanent /old/ http://centos6/new/
以下、telnetコマンドを使用してold.htmlにアクセスした時の出力です。
HTTPヘッダに HTTP/1.1 301 Moved Permanently があることが確認できます。
$ telnet centos6 80 Trying 192.168.56.6... Connected to centos6 (192.168.56.6). Escape character is '^]'. GET /old.html HTTP1.0 Connection closed by foreign host. [sudoutaka@mason ~]$ [sudoutaka@mason ~]$ telnet centos6 80 Trying 192.168.56.6... Connected to centos6 (192.168.56.6). Escape character is '^]'. GET /old.html HTTP/1.0 HTTP/1.1 301 Moved Permanently Date: Tue, 02 Apr 2013 04:19:44 GMT Server: Apache/2.2.15 (CentOS) Location: http://centos6/new.html Content-Length: 306 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://centos6/new.html">here</a>.</p> <hr> <address>Apache/2.2.15 (CentOS) Server at localhost Port 80</address> </body></html> Connection closed by foreign host. $
以上、ステータスコード301によるリダイレクトの設定方法記事でした。