Php implements 301 redirection jump code_PHP tutorial

WBOY
Release: 2016-07-13 10:44:59
Original
1032 people have browsed it

The implementation method of 301 redirection in PHP is very simple. We simply use the header to send the 301 status code, and then use the header to jump. The effect is the same as that of apache, iis, and nginx.

1: This method is more recommended because it can transfer all the original URLs of www.bKjia.c0m to the new address of bKjia.c0m

 代码如下 复制代码
$the_host = $_SERVER['HTTP_HOST'];
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
if($the_host == 'www.bKjia.c0m')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://bKjia.c0m'.$request_uri);//
}
?>

Two: Single page multi-site Php301 redirect code, www.bKjia.c0m and bKjia.c0m will 301 go to index.php, www.bKjia.c0m will 301 go to bKjia.c0m, otherwise it will go to the error page

The code is as follows Copy code
 代码如下 复制代码

if(($HTTP_HOST=="www.bKjia.c0m")or($HTTP_HOST=="bKjia.c0m"))
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: /index.php");
}
elseif($HTTP_HOST=="www.bKjia.c0m")
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://bKjia.c0m");
}
else
{
Header("Location: /404.htm");
}
?>

if(($HTTP_HOST=="www.bKjia.c0m")or($HTTP_HOST=="bKjia.c0m"))
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: /index.php");
}
elseif($HTTP_HOST=="www.bKjia.c0m")
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://bKjia.c0m");
}
else
{
Header("Location: /404.htm");
}
?>

附上其它跳转办法

//CSS header('Content-type: text/css');

 代码如下
 代码如下 复制代码

//定义编码
header( 'Content-Type:text/html;charset=utf-8 ');

//Atom
header('Content-type: application/atom+xml');

//CSS
header('Content-type: text/css');

//Javascript
header('Content-type: text/javascript');

//JPEG Image
header('Content-type: image/jpeg');

//JSON
header('Content-type: application/json');

//PDF
header('Content-type: application/pdf');

//RSS
header('Content-Type: application/rss+xml; charset=ISO-8859-1');

//Text (Plain)
header('Content-type: text/plain');

//XML
header('Content-type: text/xml');

// ok
header('HTTP/1.1 200 OK');

//设置一个404头:
header('HTTP/1.1 404 Not Found');

//设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');

//转到一个新地址
header('Location: http://www.example.org/');

//文件延迟转向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';

//当然,也可以使用html语法实现
// header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// 对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

//设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG***
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockw**e-flash'); //Flash动画

//显示登陆对话框
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';

复制代码


//定义编码
header( 'Content-Type:text/html;charset=utf-8 ');

//Atom header('Content-type: application/atom+xml');
//Javascript

header('Content-type: text/javascript');

header('Content-type: image/jpeg'); //JSON header('Content-type: application/json'); //PDF header('Content-type: application/pdf'); //RSS header('Content-Type: application/rss+xml; charset=ISO-8859-1');
//Text (Plain) header('Content-type: text/plain');
//XML
header('Content-type: text/xml'); // ok header('HTTP/1.1 200 OK'); //设置一个404头: header('HTTP/1.1 404 Not Found'); //设置地址被永久的重定向 header('HTTP/1.1 301 Moved Permanently'); //转到一个新地址 header('Location: http://www.example.org/'); //文件延迟转向: header('Refresh: 10; url=http://www.example.org/'); print 'You will be redirected in 10 seconds'; //当然,也可以使用html语法实现 //
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template