1、Linux主機重定向
Godaddy的Liunx主機,Godaddy本身已經(jīng)支持Apache,所以直接創(chuàng)建一個.htaccess文件就可以了,一般來說,在本地?zé)o法創(chuàng)建.htaccess的時候可以先創(chuàng)建一個txt格式文件,上傳到根目錄的時候再重命名為".htaccess"就可以了。網(wǎng)上很多如何進行 301重定向的教程,無論是整站重定向還是單頁重定向。下面就以我的www.jb51.net為例
1.1 無www域名轉(zhuǎn)移到www域名
復(fù)制代碼 代碼如下:
RewriteEngine on
rewritecond %{http_host} ^jb51.net [nc]
rewriterule ^(.*)$ https://www.jb51.net/$1 [r=301,nc]
1.2 整站301重定向
復(fù)制代碼 代碼如下:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jb51.net [NC]
RewriteRule ^(.*)$ https://www.jb51.net/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.jb51.net [NC]
RewriteRule ^(.*)$ http://jb51.net/$1 [L,R=301]
另外一種是在根目錄下的index.php里這樣弄
復(fù)制代碼 代碼如下:
header("HTTP/1.1 301 Moved Permanently");
header("Location:http://jb51.net/");
exit();
2、ASP主機301重定向
在 index.asp 或 default.asp 的最頂部加入以下幾行:
代碼如下:
復(fù)制代碼 代碼如下:
%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","https://www.jb51.net "
Response.End
%>
3、ASP.net主機301重定向
ASP .NET:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","https://www.jb51.net");
}
我封裝在一個類里:
復(fù)制代碼 代碼如下:
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;//是否 返回主頁 或者保留在當(dāng)前頁
/// summary>
/// 構(gòu)造函數(shù)
/// /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();
}
}
}
}
4 PHP的301重定向
復(fù)制代碼 代碼如下:
header('HTTP/1.1 301 Moved Permanently');//發(fā)出301頭部
header('Location: http://www.'.$strDomain.$request_uri);//跳轉(zhuǎn)到我的新域名地址
我用301.inc.php文件寫了301代碼,在其他文件頭部都引用上 就可以了
復(fù)制代碼 代碼如下:
?php
//-----------------------------------
//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');//發(fā)出301頭部
header('Location: http://www.'.$strDomain.$request_uri);//跳轉(zhuǎn)到我的新域名地址
}
//----------------------------------
?>
引用如下:
復(fù)制代碼 代碼如下:
?php
//-----------------------------------
//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(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","/other.jsp");
return;
%>
[code]
在apache中如何實現(xiàn)301轉(zhuǎn)向呢?
編輯.htaccess的方法。
注意:在設(shè)置301重定向之前務(wù)必備份相應(yīng)目錄下的.htaccess文件。
1.重定向domain.com到www.domain.com
這種重定向旨在使域名唯一,是網(wǎng)站SEO必須要做的,后面重定向www.domain.com到domain.com也是出于同樣的原因,只是形式不同。
打開.htaccess文件,加入以下規(guī)則。(下面的規(guī)則是針對主域名的,子域名要修改)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
2.重定向www.domain.com到domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
3.重定向olddomain.com到www.newdomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !olddomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
4.重定向olddomain.com to newdomain.com
RewriteEngine On
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
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [R=301,L]
您可能感興趣的文章:- Asp.Net實現(xiàn)404頁面與301重定向的方法
- Windows虛擬主機與VPS如何實現(xiàn)301重定向(asp.net)
- 301重定向代碼合集(iis,asp,php,asp.net,apache)
- asp,asp.net,php,jsp下的301轉(zhuǎn)向代碼
- ASP.NET中的跳轉(zhuǎn) 200, 301, 302轉(zhuǎn)向?qū)崿F(xiàn)代碼
- ASP.NET實現(xiàn)301重定向方法