asp.net php asp jsp 301重定向的代码(集合)

1、Linux主机重定向
Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般来说,在本地无法创建.htaccess的时候可以先创建一个txt格式文件,上传到根目录的时候再重命名为".htaccess"就可以了。网上很多如何进行 301重定向的教程,无论是整站重定向还是单页重定向。下面就以我的www.jb51.net为例
1.1 无www域名转移到www域名
复制代码 代码如下:

RewriteEngine on
rewritecond %{http_host} ^jb51.net [nc]
rewriterule ^(.*)$ //www.jb51.net/$1 [r=301,nc]

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

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jb51.net [NC]
RewriteRule ^(.*)$ //www.jb51.net/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.jb51.net [NC]
RewriteRule ^(.*)$ http://jb51.net/$1 [L,R=301]

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

header("HTTP/1.1 301 Moved Permanently");
header("Location:http://jb51.net/");
exit();

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

<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","//www.jb51.net "
Response.End
%>

3、ASP.net主机301重定向
ASP .NET:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","//www.jb51.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)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1576.html