介绍一下针对各类程序系统实施301重定向的代码:
1、Linux主机重定向
Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般来说,在本地无法创建.htaccess的时候可以先创建一个txt格式文件,上传到根目录的时候再重命名为“.htaccess”就可以了。网上很多如何进行 301重定向的教程,无论是整站重定向还是单页重定向。下面就以我的www.wecanwecan.com为例
1.1 无www域名转移到www域名
RewriteEngine on
rewritecond %{http_host} ^wecanwecan.com [nc]
rewriterule ^(.*)$ http://www.wecanwecan.com/$1 [r=301,nc]
1.2 整站301重定向
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wecanwecan.com [NC]
RewriteRule ^(.*)$ http://www.wecanwecan.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.wecanwecan.com [NC]
RewriteRule ^(.*)$ http://wecanwecan.com/$1 [L,R=301]
另外一种是在根目录下的index.php里这样弄
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location:http://wecanwecan.com/”);
exit();
2、ASP主机301重定向
在 index.asp 或 default.asp 的最顶部加入以下几行:
代码如下:
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”www.wecanwecan.com ”
Response.End
%>
3、ASP.net主机301重定向
ASP .NET:
Response.Status = “301 Moved Permanently”;
立即学习“PHP免费学习笔记(深入)”;
Response.AddHeader(”Location”,"http://www.wecanwecan.com");
}
我封装在一个类里:
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();
}
}
}
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号