#navi(../)
* Apacheでページが移動したことを示すステータスコード301の設定方法 [#wd7a0724]
ページが移動(引越し)してURLが変更したことを示すステータスコード301をhttpd.conf(apache.conf)に記す方法を以下に記します。~
レンタルサーバなどを使用している場合は、.htaccessに記述すれば同様の動作となります。~

#contents
#htmlinsertpcsp(web-top.html,web-sp.html)

* 関連記事 [#y23c1988]
-[[.htaccessによる設定が反映するようにする>Apache/.htaccessによる設定が反映するようにする]]

* ステータスコード301でサーチエンジン等にも対応する [#h4821186]
ページが移動(引越し)しURLを変更した場合、サーチエンジンにも反映されるようにするには、~
HTTPのステータスコード301を送ることによりサーチエンジンにページが移動したことを伝えることができます。~
また、サーチエンジンで検索し移動前のリンクをクリックしてもブラウザにステータスコード301が送信されるため、~
ブラウザも新しい引越先のページを開くようになります。

** 参考資料~ [#a87b8aec]
-[[Google - サイトを移転する>http://support.google.com/webmasters/bin/answer.py?hl=ja&answer=83105&from=34464&rd=1]]
-[[Yahoo - リダイレクトを設定している場合…>http://www.yahoo-help.jp/app/answers/detail/p/595/a_id/42722]]
 
* ステータスコード301によるリダイレクトの記述方法 [#o33a2f1c]
以下の構文でステータスコード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に移動したことを意味します。

* 実際に設定してみる [#mcdcb207]
CentOS6を使用して実際に設定してみます。~
(他のLinuxディストリビューションやWindowsでは設定ファイル名や操作方法が異なる場合がありますので読みかえてください。)
サーバー名はcentos6になっています。

** httpd.conf(apache.conf)を修正して実現してみる [#oefe45e8]
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というファイル名で設定ファイルを作成し保存しました。~
以下に簡単な例を記します。

*** ファイル単位の転送 [#se63a63a]
+ 既存HTML(old.html)~
以下のようにブラウザでold.htmlにアクセスするとOLDと表示されたページが表示されます。
#ref(01.gif)
#br
+ リダイレクトされるnew.htmlを作成する
+ redirect301.confを以下のように記述し保存する。
 Redirect permanent /old.html http://centos6/new.html
+ rootユーザでapache(httpd)を再起動する
 # service httpd restart
+ 再度old.htmlを表示するとリダイレクトされnew.htmlが表示されます。
#ref(02.gif)

複数のページを指定する場合は以下のように下に追記すれば実現できます。
 # cat /etc/httpd/conf.d/redirect301.conf 
 Redirect permanent /old.html http://centos6/new.html
 Redirect permanent /old2.html http://centos6/new2.html

*** ディレクトリ単位の転送 [#k18adc0e]
以下のようなディレクトリ構成でテストしてみます。~
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/
+ apache(httpd)を再起動します。
 # service httpd restart
+以上の設定により以下のように動作します。
++ http://centos6/old/1.html -> http://centos6/new/1.html
++ http://centos6/old/2.html -> http://centos6/new/2.html~
ディレクトリの移動なので、移動先のディレクトリには同名のhtmlファイルが存在していなければなりません。

** .htaccessを使用して実現してみる [#m92d70c5]
上記では、httpd.conf(apache.conf)を修正してステータスコード301のリダイレクトを実現していました。~
以下は、httpd.conf(apache.conf)を修正することができない場合(レンタルサーバー等)の対処方法です。~
ファイル名.htaccessを使用して実現してみます。

設定に関しては上記のhttpd.conf修正と同じにしました。~
.htaccessファイルが存在しない場合は作成してください。
+ .htaccessに以下の内容を記述しました。
 Redirect permanent /old.html http://centos6/new.html
 Redirect permanent /old/ http://centos6/new/
+ 上記の設定により以下の動作になります。
++ http://centos6/old.html -> http://centos6/new.html
++ http://centos6/old/1.html -> http://centos6/new/1.html
++ http://centos6/old/2.html -> http://centos6/new/2.html

* telnetコマンドを使用してHTTPヘッダを調査した結果 [#ae8382a0]
以下、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によるリダイレクトの設定方法記事でした。

#htmlinsertpcsp(web-btm.html,web-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS