Php实现301重定向跳转代码
header常用指令
header分为三部分: 第一部分为HTTP协议的版本(HTTP-Version);
第二部分为状态代码(Status);
第三部分为原因短语(Reason-Phrase)。
一:更推荐这种方法,因为它可以把www.111cn.net原来所有的url都转到111cn.net新的地址上
// fix 404 pages: 用这个header指令来解决URL重写产生的404 header header('HTTP/1.1 200 OK');
代码如下
// set 404 header: 页面没找到 header('HTTP/1.1 404 Not Found');
<?php
$the_host = $_SERVER['HTTP_HOST'];
$request_uri =
isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
if($the_host == 'www.111cn.net')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://111cn.net'.$request_uri);//
}
?>
//页面被永久删除,可以告诉搜索引擎更新它们的urls // set Moved Permanently header (good for redrictions)
// use with location header
header('HTTP/1.1 301 Moved Permanently');
二:单页多站的Php301重定向代码,www.111cn.net和111cn.net则301到index.php上,www.111cn.net则301到111cn.net上,否则转到错误页
// 访问受限 header('HTTP/1.1 403 Forbidden');
代码如下
// 服务器错误 header('HTTP/1.1 500 Internal Server Error');
if(($HTTP_HOST=="www.111cn.net")or($HTTP_HOST=="111cn.net"))
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: /index.php");
}
elseif($HTTP_HOST=="www.111cn.net")
{
header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://111cn.net");
}
else
{
Header("Location: /404.htm");
}
?>
// 重定向到一个新的位置 // redirect to a new location:
header('Location: http://www.example.org/');
附上其它跳转办法
延迟一段时间后重定向 // redrict with delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
代码如下
// 覆盖 X-Powered-By value // override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');
//定义编码
header( 'Content-Type:text/html;charset=utf-8 ');
// 内容语言 (en = English) // content language (en = English)
header('Content-language: en');
//Atom
header('Content-type: application/atom xml');
//最后修改时间(在缓存的时候可以用到) // last modified (good for caching)
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
//css
header('Content-type: text/css');
// 告诉浏览器要获取的内容还没有更新 // header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');
//Javascript
header('Content-type:
text/javascript');
// 设置内容的长度 (缓存的时候可以用到): // set content length (good for caching):
header('Content-Length: 1234');
//JPEG Image
header('Content-type: image/jpeg');
// 用来下载文件: // Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
//JSON
header('Content-type: application/json');
// 禁止缓存当前文档: // load the file to send:readfile('example.zip');
// Disable caching of the current document:
header('Cache-Control: no-cache, no-store, max-age=0,
must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
//PDF
header('Content-type: application/pdf');
// 设置内容类型: // Date in the pastheader('Pragma: no-cache');
// set content type:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain');
//RSS
header('Content-Type: application/rss xml; charset=ISO-8859-1');
// plain text file
header('Content-Type: image/jpeg');
//Text (Plain)
header('Content-type: text/plain');
本文由澳门娱乐场网址发布于计算机教程,转载请注明出处:Php实现301重定向跳转代码
关键词:
上一篇:javascript动态判断html元素并执行不同的操作
下一篇:没有了