本文将汇总各类各类主机系统实施301重定向的代码。文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
1、Linux主机重定向
Godaddy的Liunx主机,Godaddy本身已经支持Apache,所以直接创建一个.htaccess文件就可以了,一般来说,在本地无法创建.htaccess的时候可以先创建一个txt格式文件,上传到根目录的时候再重命名为“.htaccess”就可以了。网上很多如何进行 301重定向的教程,无论是整站重定向还是单页重定向。下面就以我的www.duoluodeyu.com为例
1.1 无www域名转移到www域名
RewriteEngine on
rewritecond %{http_host} ^duoluodeyu.com [nc]
rewriterule ^(.*)$ http://www.duoluodeyu.com/$1 [r=301,nc]
1.2 整站301重定向
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^duoluodeyu.com [NC]
RewriteRule ^(.*)$ http://www.duoluodeyu.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.duoluodeyu.com [NC]
RewriteRule ^(.*)$ http://duoluodeyu.com/$1 [L,R=301]文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
另外一种是在根目录下的index.php加入如下代码:文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location:http://duoluodeyu.com/”);
exit();文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
2、ASP主机301重定向文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
在 index.asp 或 default.asp 的最顶部加入以下几行:
代码如下:文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”,”www.duoluodeyu.com ”
Response.End
%>文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
3、ASP.net主机301重定向文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
ASP .NET:文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
Response.Status = “301 Moved Permanently”;文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
Response.AddHeader(”Location”,"http://www.duoluodeyu.com");文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
}文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
我封装在一个类里:文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
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();
}
}
}
}文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
4 PHP的301重定向文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location: http://www.'.$strDomain.$request_uri);//跳转到我的新域名地址文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
我用301.inc.php文件写了301代码,在其他文件头部都引用上 就可以了文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
<?php
//-----------------------------------
//301 重定向
$strDomain="duoluodeyu.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);//跳转到我的新域名地址
}文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
//----------------------------------
?>文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
引用如下:文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
<?php
//-----------------------------------
//301 重定向
include('include/301.inc.php');文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
?>文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
5 JSP的301重定向文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
如一页面article.jsp文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","/other.jsp");文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
return;
%>文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
在apache中如何实现301转向呢?文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
编辑.htaccess的方法。
注意:在设置301重定向之前务必备份相应目录下的.htaccess文件。
1.重定向domain.com到www.domain.com
这种重定向旨在使域名唯一,是网站SEO必须要做的,后面重定向www.domain.com到domain.com也是出于同样的原因,只是形式不同。
打开.htaccess文件,加入以下规则。(下面的规则是针对主域名的,子域名要修改)文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteEngine On
文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
2.重定向www.domain.com到domain.com文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteEngine On
文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
3.重定向olddomain.com到www.newdomain.com文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteEngine On
文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteCond %{HTTP_HOST} !olddomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
4.重定向olddomain.com to newdomain.com文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteEngine On
文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteBase /
RewriteCond %{HTTP_HOST} !olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
5.重定向domain.com/file/file.php 到 otherdomain.com/otherfile/other.php文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteCond %{HTTP_HOST} ^www.domain.com$
文章源自堕落的鱼-https://www.duoluodeyu.com/827.html
RewriteRule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [R=301,L]