php 实现301重定向跳转实例代码
本文主要介绍php 实现301重定向跳转,通过实例代码让大家更好的理解重定向的方法,有需要的小伙伴可以参考下
在php中301重定向实现方法很简单我们只要简单的利用header发送301状态代码,然后再用header进行跳转,效果与apache,iis,nginx都是一样的效果哦。
一:更推荐这种方法,因为它可以把http://www.jb51.net原来所有的url都转到http://jb51.net新的地址上
代码如下:
<?php
$the_host
=
$_SERVER
[
'HTTP_HOST'
];
$request_uri
= isset(
$_SERVER
[
'REQUEST_URI'
]) ?
$_SERVER
[
'REQUEST_URI'
] :
''
;
if
(
$the_host
==
'www.jb51.net'
)
{
header(
'HTTP/1.1 301 Moved Permanently'
);
}
?>
if
((
$HTTP_HOST
==
"www.jb51.net"
)
or
(
$HTTP_HOST
==
"jb51.net"
))
{
header(
"HTTP/1.1 301 Moved Permanently"
);
Header(
"Location: /index.php"
);
}
elseif
(
$HTTP_HOST
==
"jbzj.com"
)
{
header(
"HTTP/1.1 301 Moved Permanently"
);
Header(
"Location:<strong> </strong>www.jbzj.com"
);
}
else
{
Header(
"Location: /404.htm"
);
}
?>
附上其它跳转办法
代码如下:
//定义编码
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'
);
//转到一个新地址
//文件延迟转向:
print
'You will be redirected in 10 seconds'
;
//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
// override X-Powered-By: PHP:
header(
'X-Powered-By: PHP/4.4.0'
);
header(
'X-Powered-By: Brain/0.6b'
);
//文档语言
header(
'Content-language: en'
);
//告诉浏览器最后一次修改时间
$time
= time() - 60;
// or filemtime($fn), etc
header(
'Last-Modified: '
.
gmdate
(
'D, d M Y H:i:s'
,
$time
).
' GMT'
);
//告诉浏览器文档内容没有发生改变
header(
'HTTP/1.1 304 Not Modified'
);
//设置内容长度
header(
'Content-Length: 1234'
);
//设置为一个下载类型
header(
'Content-Type: application/octet-stream'
);
header(
'Content-Disposition: attachment; filename="example.zip"'
);
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'
;
跳转要注意以下几点,有助于解决一些新手经常遇到的问题
1、location和“:”号间不能有空格,否则会出错。
2、在用header前不能有任何的输出。
3、header后的PHP代码还会被执行。
以上就是,对php 301重定向跳转的资料整理,希望能帮助php 开发的小伙伴们。
出处至:http://www.jb51.net/article/88785.htm
php 实现301重定向跳转实例代码的更多相关文章
- 常用nginx rewrite重定向-跳转实例:
1,将www.myweb.com/connect 跳转到connect.myweb.com rewrite ^/connect$ http://connect.myweb.com permanent; ...
- http方式nginx 访问不带www的域名301重定向跳转到www的域名帮助seo集中权重
比如我需要吧gucanhui.com重定向301跳转到www.gucanhui.com 需要在nginx的con发文件中加入一段 server { listen ; server_name gucan ...
- https方式nginx 代理tomcat访问不带www的域名301重定向跳转到www的域名帮助seo集中权重
比如我要把 http://gucanhui.com http://www.gucanhui.com 跳转到https://www.gucanhui.com 用F12的network可以看到状态码301 ...
- .htaccess的301重定向代码
把不带www的域名301到带www的域名 RewriteEngine On RewriteCond %{http_host} ^example.com$ [NC] RewriteRule ^(.*)$ ...
- 301重定向.htaccess规则(含二级目录跳转二级域名)
301重定向是一种非常重要的"自动转向"技术.网址重定向最为可行的一种办法.当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码 ...
- asp.net php asp jsp 301重定向的代码
介绍一下针对各类程序系统实施301重定向的代码: 1.Linux主机重定向 Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般 ...
- php 网站301重定向设置代码实战案例
php 网站301重定向设置代码实战案例 301重定向就是页面永久性移走的意思,搜索引擎知道这个页面是301重定向的话,就会把旧的地址替换成重定向之后的地址. 302重定向就是页面暂时性转移,搜索引擎 ...
- Haproxy 重定向跳转设置 - 运维小结
前面已经详细介绍了Haproxy基础知识 , 今天这里再赘述下Haproxy的重定向跳转的设置. haproxy利用acl来实现haproxy动静分离,然而在许多运维应用环境中,可能需要将访问的站点 ...
- PHP使用header+Location实现网站301重定向
对于我们SEO人员来说,有的时候需要对网站进行重定向.一般来说,对网站重定向的http返回状态码是301和302两种.下面兰州SEO就通过实例来为你介绍一下怎样通过PHP使用header+Locati ...
随机推荐
- Luogu P1004/P1006 方格取数/传纸条 【棋盘Dp】 By cellur925
我明明记得写过这篇啊qwq为什么会搞丢 两题几乎一样. 如果再拓展到k条路,就要用网络流跑了,本蒟现在还不会. 我们容易想到四维dp,但是有一种更好的方法. 首先,先从左上到右下.再从右下到左上可以近 ...
- WPF-DataGrid(数据表格)美化
我们不多哔哔先上图: 数据表格使用背景: 当我们在做二次开发发现我我们的表格无法向WEB的表格一样好看,这时我们就需要对数据表格进行美化和重构 表格美化思维引导: WPF数据表格是由表头和表体(内容) ...
- [Usaco2006 Jan] Dollar Dayz 奶牛商店
Description 约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,-,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢? Inpu ...
- _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...
- Thinkpad x230设置启动顺序
设置可以从CD或者USB启动1.F1进入BIOS,Security → Secure Boot ,设置为:Disabled2.Startup → UEFI/Legacy Boot ,设置为:Both( ...
- 用for循环实现的菱形图案
package com.wh.lingxing; import java.util.Scanner; public class LingXing { public static void main(S ...
- spring tool suite开发环境搭建
先把是构建工具maven: maven里面有一个conf文件夹,然后里面有个setting.xml配置文件,先要把项目要的setting.xml覆盖这个原来的配置文件. 这个maven配置文件有一个作 ...
- 转】MYSQL性能调优与架构设计之select count(*)的思考
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/5/ 感谢! Posted: Feb 7, 2013 Tag ...
- js点击修改按钮后修改
<button id="click">改变内容</button> <div id="t">要改变的内容</div> ...
- 一个简单的公式——求小于N且与N互质的数的和
首先看一个简单的东西. 若$gcd(i,n)=1$,则有$gcd(n-i,n)=1$ 于是在小于$n$且与$n$互质的数中,$i$与$n-i$总是成对存在,且相加等于$n$. 考虑$i=n-i$的特殊 ...