URL Redirect 网址重定向的方法

在将网站更换成新网址的情况下,可能会在旧网址上使用到一些『转导网址』的方法,以便将原本的使用者及其流量引导到新的网址去。

以下整理、讨论到几种转导(Redirect)网址的技术方法,并且探讨该方法对 SEO 的影响:

1. 使用 HTTP 通讯协议 301 Moved Permanently 来完成转导网址 (永久转址)

(建议使用,不会对 SEO 有不良影响)

o PHP 程序范例:

The document has moved here.

注1:

使用者的浏览器必须根据 HTTP header 的 Location 字段值(称做URI)来转导网址。

注2:

除非 Request Method 是 HEAD,不然服务器端回复的讯息内必须包含一短的新网址的连结(hyperlink)信息。

o ASP 程序范例:

<%@ Language=VBScript %>
<% Response.Status="301 Moved Permanently" Response.AddHeader "Location", " http://www.yaoyingying.com/" Response.End %>

The document has moved here.

o ASP.NET 程序范例:

The document has moved here.

o 在 .htaccess / httpd.conf 档案中设定 — 转整个 domain

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*) http://www.new-domain.com/$1 [R=301,L]

o 在 .htaccess / httpd.conf 档案中设定 — 转到新的 www.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^old-domain.com [NC]
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,NC]

2. 使用 HTTP/1.1 通讯协议 302 Found 来完成转导网址

(不建议使用,会对新网站 SEO 有不良影响)

o PHP 程序范例:

The document has moved here.

(…其它 ASP, ASP.NET 程序及设定 .htaccess/httpd.conf 方法,此处略 …)

注1: 302,

在 HTTP/1.0 是『Moved Temporarily』;HTTP/1.1 是『Found』,会根据 HTTP header 的 Location 字段值(称做URI)来转导网址。但是很多网络上的文章会直接称 302 是 Moved Temporatily。

注2:

除非 Request Method 是 HEAD,不然服务器端回复的讯息内必须包含一短的新网址的连结(hyperlink)信息。

注3:

HTTP 1.1 中增订了 『307 Temporary Redirect』,307 码时只会根据 GET Request 转导网址。

注4:

更多的 HTTP 302 细节和 307 会被再增订出来的原因请参考(http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)。

3. HTML 的 refresh meta tag 来转导网址

(非常不建议使用,会对新网站 SEO 有不良影响。有些文章写说要用时最好秒数设定大于 10 秒以避免对页面的 SEO 不利。)

o 在 HTML 档案的 HEAD 中,范例:




4. 用 JavaScript 来达到转导网址 (放在 HTML 的…或…中

(因为搜寻引擎的 bot 一般都不理会 JavaScript,所以做什么动作不会被检查。这意味着要实做『点击计算(click counting)后再转导到目的网址的话,用这个方法比较好(302 或 refresh 都是不好的方法)』)
(如果使用者按浏览器的『上一页』按钮,不会跳回转导页面。)

o 直接在 HTML 的 HEAD 中用转导网址 JavaScript 范例:

o JavaScript 内容同上例,但是把它放到外部的一个 .js 档案,然后…中只要写:

o 也是使用 JavaScript,但是额外透过『窗体』来完成:

(因为搜寻引擎的 bot 一般都不理会『窗体』,所以做什么动作不会被检查。)

额外讨论:

301/302 有时会被一些人用作旁门走道方法,在玩『PR劫持』(如这篇文章所述(http://www.chinamyhosting.com/seoblog/2006/12/08/pr-hijack/),更多的一些手法讨论请看这篇文章(http://www.loriswebs.com/hijacking_web_pages.html)或用 hijack 当 KeyWord 去查查。

302 在之前会造成 bot 误以为是转导到的网站在恶搞,而将转导到的网站从索引中除名。所以会变得无法防止别人以此方法攻击自己的 URL。现或许已更正。(详情请看http://www.tonyspencer.com/2004/12/10/tracker2php-pagejacking-via-http-302-redirect-google-bug/ )

当然,refresh 也能如上述 302 一样去恶搞别人的网站。

这篇文章的关键词(Keyword): http redirection, http redirect head, http redirects, http redirect html, http redirect example, http redirect meta, redirect script, redirect php, redirect javascript, html refresh page, html redirection, html redirect page, 301 redirect, 302 redirect
文章来自 (http://rental.zhupiter.com/postshow_184_1_1_1.html)