基本
あるページをアクセスしようとすると別のページに飛ばせることを,リダイレクトといいます。
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
FROM :