Archive

Archive for 3 月, 2009

リダイレクト

3 月 19th, 2009

基本
あるページをアクセスしようとすると別のページに飛ばせることを,リダイレクトといいます。
PHPを使わなくてもリダイレクトできますが,PHPを使えば条件に応じて違うところにリダイレクトすることもできます。
まずは無条件にリダイレクトする方法です。ファイルにはこれ以外何も書きません。
<?php
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://example.jp/”);
?>

応用
http://www.example.com/ でも http://example.com/ でもアクセスできるページをどちらかに統一します:
<?php
if (strtolower($_SERVER['HTTP_HOST']) == ‘example.com’) {
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.example.com$_SERVER[REQUEST_URI]“);
exit();
}
?>

http://www.example.com/ でも http://www.example.com/index.php でもアクセスできるページをどちらかに統一します:
<?php
if ($_SERVER['REQUEST_URI'] == ‘/index.php’) {
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.example.com/”);
exit();
}
?>

参考リンク
Steven Hargrove : How to redirect a web page, the smart way
onPHP5.com - Some SEO Tips You Would Not Like to Miss
-

奥村晴彦>/a>
Last modified: 2007-08-04 22:31:51

admin PHP