网上大多数有关301重定向是通过.htaccess来实现的,但这样仅适用于一部分服务器软件

现在来个PHP版的,应该可以适用于绝大多数PHP环境了

<?php
$the_host = $_SERVER['HTTP_HOST'];
$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$the_url = strtolower($the_url);
if($the_url=="/index.php")
{
header('HTTP/1.1 301 Moved Permanently');
header('Location:http://www.izhuyue.com');
}
if($the_url !=="/index.php")
{
header('HTTP/1.1 301 Moved Permanently');
header('Location:http://www.izhuyue.com'.$the_url);
}
?>

将其中的域名换成自己的就可以了,这段代码应该可以在所有支持PHP的空间完美运行

目前这段代码被托管在BAE,并且绑定了izhuyue.cn这个域名。访问这个cn域名的页面,看看是不是可以完美重定向至com域名网站:http://www.izhuyue.com/php-301-redirect/

标签: none

已有 2 条评论

  1. 光从这段来看,the_host无意义吧。

    1. FROYO

      貌似确实没啥用

评论已关闭