一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net php jsp asp 301重定向实现代码

时间:2022-06-24 23:59:46 编辑:袖梨 来源:一聚教程网

1.1 无www域名转移到www域名
复制代码 代码如下:

rewriteengine on
rewritecond %{http_host} ^111com.net [nc]
rewriterule ^(.*)$ http://www.111com.net/$1 [r=301,nc]

1.2 整站301重定向
复制代码 代码如下:

options +followsymlinks
rewriteengine on
rewritecond %{http_host} ^111com.net [nc]
rewriterule ^(.*)$ http://www.111com.net/$1 [l,r=301]
rewritecond %{http_host} ^www.111com.net [nc]
rewriterule ^(.*)$ http://111com.net/$1 [l,r=301]

另外一种是在根目录下的index.php教程里这样弄
复制代码 代码如下:

header(“http/1.1 301 moved permanently”);
header(“location:http://111com.net/”);
exit();

2、asp教程主机301重定向
在 index.asp 或 default.asp 的最顶部加入以下几行:
代码如下:
复制代码 代码如下:

<%
response.status=”301 moved permanently”
response.addheader “location”,”www.111com.net ”
response.end
%>

3、asp.net教程主机301重定向
asp .net:

response.status = “301 moved permanently”;
response.addheader(”location”,"http://www.111com.net");
}


我封装在一个类里:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.web.ui;
using system.web.ui.htmlcontrols;
namespace classlib
{
public class urlclass
{
private bool flag301 = false;//是否启动 301
private bool isindex = false;//是否 返回主页 或者保留在当前页
///
/// 构造函数
///

/// 是否启动 301
/// page
/// 格式www.xxx.com
public urlclass(bool fl, page page, string strurl)
{
flag301 = fl;
url301(page, strurl);
}
///
/// 返回主页
///

///
/// 格式www.xxx.com
public void url301(page page, string strurl)
{
//301重定向
if (page.request.url.dnssafehost != strurl && flag301 == true)
{
page.response.clear();
page.response.statuscode = 301;
page.response.status = "301 movedpermanently";
page.response.addheader("location", "http://" + strurl);
page.response.end();
}
}
}
}

4 php的301重定向
复制代码 代码如下:

header('http/1.1 301 moved permanently');//发出301头部
header('location: http://www.'.$strdomain.$request_uri);//跳转到我的新域名地址

我用301.inc.php文件写了301代码,在其他文件头部都引用上 就可以了
复制代码 代码如下:

//-----------------------------------
//301 重定向
$strdomain="chinawecan.com";
$the_host = $_server['http_host']; //取得进入所输入的域名
$request_uri = isset($_server['request_uri']) ? $_server['request_uri'] : '';//判断地址后面部分
if($the_host !== 'www.'.$strdomain) //这是我要以前的域名
{
/*“!==”是不完全等于的意思,也可以用“!=”不等于,这样,就可以将以前的域名,
包括gcxirang.com、www.gcxirang.com以及新域名中我gcidc.net全部重定向到www.gcidc.net*/
header('http/1.1 301 moved permanently');//发出301头部
header('location: http://www.'.$strdomain.$request_uri);//跳转到我的新域名地址
}
//----------------------------------
?>

引用如下:
复制代码 代码如下:

//-----------------------------------
//301 重定向
include('include/301.inc.php');
?>

5 jsp教程的301重定向
如一页面article.jsp
[code]
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<%
response.setstatus(https教程ervletresponse.sc_moved_permanently);
response.setheader("location","/other.jsp");
return;
%>

热门栏目