[WEB][PHP][SEO] 轉導、轉向(Redirect)網址的方法.

[WEB][PHP][SEO] 轉導、轉向(Redirect)網址的方法

在將網站更換成新網址的情況下,可能會在舊網址上使用到一些『轉導網址』的方法,以便將原本的使用者及其流量引導到新的網址去。

以下整理、討論到幾種轉導(Redirect)網址的技術方法,並且探討該方法對 SEO 的影響:

1. 使用 HTTP 通訊協定 301 Moved Permanently 來完成轉導網址 (永久轉址)

(建議使用,不會對 SEO 有不良影響)

o PHP 程式範例:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.new-url.com/”);
?>
<p>The document has moved <a href=”http://www.new-url.com/”>here</a>.</p>

  • 註1: 使用者的瀏覽器必須根據 HTTP header 的 Location 欄位值(稱做URI)來轉導網址。
  • 註2:除非 Request Method 是 HEAD,不然伺服器端回覆的訊息內必須包含一短的新網址的連結(hyperlink)資訊。

o ASP 程式範例:

<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, ” http://www.new-url.com/”
Response.End
%>
<p>The document has moved <a href=”http://www.new-url.com/”>here</a>.</p>

o ASP.NET 程式範例:

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.new-url.com/”);
}
</script>
<p>The document has moved <a href=”http://www.new-url.com/”>here</a>.</p>

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]

o 在 apache 的 httpd-vhosts.conf 設定轉導 (redirect)

<VirtualHost 61.62.63.64:80>
ServerName zhupiter.com
Redirect 301 / http://www.zhupiter.com/
</VirtualHost>

適用於一個 IP 多個 domainnames 一起用的主機。

2. 使用 HTTP/1.1 通訊協定 302 Found 來完成轉導網址

(建議使用,會對新網站 SEO 有不良影響)

o PHP 程式範例:

<?php
header(“HTTP/1.1 302 Found”);
header(“Location: http://www.new-url.com/”);
?>
<p>The document has moved <a href=”http://www.new-url.com/”>here</a>.</p>

(…其他 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 會被再增訂出來的原因請參考這裡

3. HTML 的 refresh meta tag 來轉導網址

(非常不建議使用,會對新網站 SEO 有不良影響。有些文章寫說要用時最好秒數設定大於 10 秒以避免對頁面的 SEO 不利。)

o 在 HTML 檔案的 HEAD 中,範例:

<html>
<head>
<meta http-equiv=”refresh” content=”0;url=http://www.new-url.com/” />
</head>

4. 用 JavaScript 來達到轉導網址 (放在 HTML 的 <head>…</head> 或 <body>…<body> 中

(因為搜尋引擎的 bot 一般都不理會 JavaScript,所以做什麼動作不會被檢查。這意味著要實做『點擊計算(click counting)後再轉導到目的網址的話,用這個方法比較好(302 或 refresh 都是不好的方法)』)

(如果使用者按瀏覽器的『上一頁』按鈕,不會跳回轉導頁面。)

o 直接在 HTML 的 HEAD 中用轉導網址 JavaScript 範例:

<html>
<head>
<script language=”JavaScript”>
<!–
window.location.replace(“http://www.new-url.com”);
//–>
</script>
</head>
</html>

o JavaScript 內容同上例,但是把它放到外部的一個 .js 檔案,然後 <head>…</head> 中只要寫:

<html>
<head>
<script language=”JavaScript” src=”redirect.js”></script>
</head>

o 也是使用 JavaScript,但是額外透過『表單』來完成:
(因為搜尋引擎的 bot 一般都不理會『表單』,所以做什麼動作不會被檢查。)

<script language=”JavaScript”>
<!–
document.myform.submit();
//–>
</script>
<form name=”myform” action=”http://www.new-url.com/” method=”get”></form>


額外討論:

  1. 301/302 有時會被一些人用作旁門走道方法,在玩『PR劫持』(如這篇文章所述),更多的一些手法討論請看這篇文章或用 hijack 當 KeyWord 去查查。
  2. 302 在之前會造成 bot 誤以為是轉導到的網站在惡搞,而將轉導到的網站從索引中除名。所以會變得無法防止別人以此方法攻擊自己的 URL。現或許已更正。(詳情請看這裡)
  3. 當然,refresh 也能如上述 302 一樣去惡搞別人的網站。

HTTP 狀態碼(status code)定義:

來源: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

[Informational 1xx]
100=”Continue”
101=”Switching Protocols”

[Successful 2xx]
200=”OK”
201=”Created”
202=”Accepted”
203=”Non-Authoritative Information”
204=”No Content”
205=”Reset Content”
206=”Partial Content”

[Redirection 3xx]
300=”Multiple Choices”
301=”Moved Permanently”
302=”Found”
303=”See Other”
304=”Not Modified”
305=”Use Proxy”
306=”(Unused)”
307=”Temporary Redirect”

[Client Error 4xx]
400=”Bad Request”
401=”Unauthorized”
402=”Payment Required”
403=”Forbidden”
404=”Not Found”
405=”Method Not Allowed”
406=”Not Acceptable”
407=”Proxy Authentication Required”
408=”Request Timeout”
409=”Conflict”
410=”Gone”
411=”Length Required”
412=”Precondition Failed”
413=”Request Entity Too Large”
414=”Request-URI Too Long”
415=”Unsupported Media Type”
416=”Requested Range Not Satisfiable”
417=”Expectation Failed”

[Server Error 5xx]
500=”Internal Server Error”
501=”Not Implemented”
502=”Bad Gateway”
503=”Service Unavailable”
504=”Gateway Timeout”
505=”HTTP Version Not Supported”


用 PHP 實作帶其他狀態碼的轉到方式:

來源: http://tw.php.net/header

Dylan at WeDefy dot com
13-Oct-2007 03:17
A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header().

<?php
// 301 永久轉址 (Moved Permanently)
header(“Location: /foo.php”, TRUE, 301);

// 302 向瀏覽器表示有找到頁面(Found), 但是跳轉/重導轉址
header(“Location: /foo.php”, TRUE, 302);
header(“Location: /foo.php”);

// 303 向瀏覽器表示請看另一頁面(See Other), 而跳轉/重導轉址
header(“Location: /foo.php”, TRUE, 303);

// 307 暫時性轉址(Temporary Redirect)
header(“Location: /foo.php”, TRUE, 307);
?>

The HTTP status code changes the way browsers and robots handle redirects, so if you are using header(Location:) it’s a good idea to set the status code at the same time.  Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely.  Search engines typically transfer “page rank” to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header(‘Location:’) defaults to 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

One thought on “[WEB][PHP][SEO] 轉導、轉向(Redirect)網址的方法.”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.