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