一, 新建一个301.php文件放在与index.php同级目录下或者其他地方也行, 301.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') {
$the_url = '';
}
//如果域名是不带www的网址那么进行下面的301跳转
if ($the_host == 'bluestep.cc') {
//发出301头部
header('HTTP/1.1 301 Moved Permanently');
//跳转到带www的网址
header('Location:http://www.bluestep.cc'.$the_url);
exit();
}
二, 在index.php文件最顶端引用301.php文件, 参考代码如下:
/** Jump to www domain */
require_once( dirname(__FILE__) . '/301.php' );