301重定向代码大全

2025-09-27 15:00:43

1、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.123049.net')//如果域名不是带www的网址那么进行下面的301跳转

{

header('HTTP/1.1 301 Moved Permanently');//发出301头部 

header('Location:http://www.youduan.net/'.$the_url);//跳转到带www的网址

}

?>

2、html  301重定向代码,可以在页头加入如下代码:

<meta http-equiv="refresh" content="0; url=http://www.youduan.net/">

3、ASP  301重定向代码,

<%

Response.Status="301 Moved Permanently"

Response.AddHeader "Location","http://www.youduan.net/"

Response.End

%>

4、Apache下301转向代码 ,新建.htaccess文件,输入下列内容(需要开启mod_rewrite): 

RewriteCond %{HTTP_HOST} ^youduan.net [NC] 

RewriteRule ^(.*)$ http://www.youduan.net/$1 [L,R=301] 

5、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.youduan.net/ [R,I]

ISAPI_Rewrite 2.X的代码如下:

[ISAPI_Rewrite]

CacheClockRate 3600

RepeatLimit 32

RewriteCond Host: ^youduan.net$

RewriteRule (.*) http://www.youduan.net/$1 [I,RP]

ISAPI_Rewrite 3.X的代码如下:

[ISAPI_Rewrite]

CacheClockRate 3600

RepeatLimit 32

RewriteCond %{HTTP:Host} ^youduan.net$

RewriteRule (.*) http://www.youduan.net/$1 [NC,R=301]

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢