什么是301重定向呢?说白了就是通过各种的方法将各种网络请求重新定个方向转到其它位置。当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久性转移到另一个地址。
php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 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="/";//如果是首页,赋值为“/”
}
if($the_host !== 'www.ganzhilin.net')//如果域名不是带www的网址那么进行下面的301跳转
{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location:http://www.ganzhilin.net/'.$the_url);//跳转到带www的网址
}
?>
|
html
1 2 3
| html 301重定向代码,可以在页头加入如下代码:
<meta http-equiv="refresh" content="0; url=http://www.ganzhilin.net/">
|
ASP
1 2 3 4 5 6 7 8 9 10 11
| ASP 301重定向代码,
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.ganzhilin.net/"
Response.End
%>
|
Apache
1 2 3 4 5
| Apache下301转向代码 ,新建.htaccess文件,输入下列内容(需要开启mod_rewrite):
RewriteCond %{HTTP_HOST} ^youduan.net [NC]
RewriteRule ^(.*)$ http://www.ganzhilin.net/$1 [L,R=301]
|
http.ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| http.ini伪静态规则的主机是最让人头疼的,因为有三个版本的伪静态规则:ISAPI_Rewrite 1.X,ISAPI_Rewrite2.X,ISAPI_Rewrite 3.x总共三个版本.选择适合自己的。代码如下。
ISAPI_Rewrite 1.X的代码如下:
[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host: ^youduan.net$
RewriteRule (.*) http://www.ganzhilin.net/ [R,I]
ISAPI_Rewrite 2.X的代码如下:
[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host: ^youduan.net$
RewriteRule (.*) http://www.ganzhilin.net/$1 [I,RP]
ISAPI_Rewrite 3.X的代码如下:
[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32
RewriteCond %{HTTP:Host} ^youduan.net$
RewriteRule (.*) http://www.ganzhilin.net/$1 [NC,R=301]
|