301重定向实现代码大全
时间:2010-11-23 来源:秩名
网上很多如何进行 301重定向的教程,无论是整站重定向还是单页重定向。下面就以我的www.111cn.net为例,因为很多时候你没有独立的服务器或你的apache不支持.haccess文件等原因,你不得不利用脚本语言来实例301重定向了。
1.1 无www域名转移到www域名
代码如下:
rewriteengine on
rewritecond %{http_host} ^111cn.net [nc]
rewriterule ^(.*)$ http://www.111cn.net/$1 [r=301,nc] 1.2 整站301重定向
代码如下:
options +followsymlinks
rewriteengine on
rewritecond %{http_host} ^111cn.net [nc]
rewriterule ^(.*)$ http://www.111cn.net/$1 [l,r=301]
rewritecond %{http_host} ^www.111cn.net [nc]
rewriterule ^(.*)$ http://111cn.net/$1 [l,r=301] 另外一种是在根目录下的index.php教程里这样弄
代码如下:
header(“http/1.1 301 moved permanently”);
header(“location:http://111cn.net/”);
exit(); 2、asp教程主机301重定向
在 index.asp 或 default.asp 的最顶部加入以下几行:
代码如下:
代码如下:
<%
response.status=”301 moved permanently”
response.addheader “location”,”www.111cn.net ”
response.end
%>
3、asp.net主机301重定向
asp .net:
response.status = “301 moved permanently”;
response.addheader(”location”,"http://www.111cn.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;//是否 返回主页 或者保留在当前页
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fl">是否启动 301</param>
/// <param name="page">page</param>
/// <param name="strurl">格式www.xxx.com</param>
public urlclass(bool fl, page page, string strurl)
{
flag301 = fl;
url301(page, strurl);
}
/// <summary>
/// 返回主页
/// </summary>
/// <param name="page"></param>
/// <param name="strurl">格式www.xxx.com</param>
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();
}
}
}
}
标签分类: