Webpage Redirection (網頁重新導向)

新增網頁法:

以下內容引用自《[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>

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 方法,此處略 …)

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>

額外討論:

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);

?>

設定 .htaccess 來達成此目的:

以下文字引用自《改變網址而不會出現「404 找不到網頁」:301 Redirect 與 .htaccess》:

當你看到這一篇教學文的時候,這個部落格的網址已經由 http://vinta.ws/wordpress/ 變成 http://vinta.ws/blog/ 了,雖然兩者的差別不是挺大的,但是,這是 奇檬子(ki mo chi)的問題。

 

現在你在網址列輸入 http://vinta.ws/wordpress/ 的話,它會自動被轉換成 http://vinta.ws/blog/,因為我使用了 301 Redirect(Permanent Redirect:永久性重新定址)。在沒有使用 Redirect 的情況下,如果就冒然地把網址改成 http://vinta.ws/blog/ 的話,那些從 http://vinta.ws/wordpress/ 連進來的人就只會看到「HTTP 404 找不到網頁」。

 

方法就是直接更改 .htaccess,這個檔案通常會在網站的根目錄,如果沒有,就自己用 Notepad 新增一個。你的作業系統不允許 .htaccess 這樣的檔案名稱時,就先把它命名為 htaccess.txt,上傳到 FTP 之後,再把檔案名稱改成 .htaccess。

 

--------------- 網頁伺服器必須是 Apache ---------------

 

【情況一】

 

http://your_domain.com/wordpress >> http://your_domain.com/blog

 

讓連接到 /wordpress 的連結重新定址到 /blog,包含下層路徑

例如:http://vinta.ws/wordpress/?p=334 會被指向 http://vinta.ws/blog/?p=334

 

在 .htaccess 中要這麼寫:

 

Redirect /wordpress http://your_domain.com/blog

 

如果有安裝 mod_rewrite 模組的話,也可以這樣寫:

 

RewriteEngine on

RewriteRule ^wordpress(.*)$ /blog$1 [R=301,L]

 

【情況二】

 

http://your_domain.com/wordpress >> http://your_domain.com

 

讓連接到 /wordpress 的連結重新定址到 根目錄,包含下層路徑(如 /wordpress/xxx)

 

在 .htaccess 中要這麼寫:

 

Redirect /wordpress http://your_domain.com

 

如果有安裝 mod_rewrite 模組的話,也可以這樣寫:

 

RewriteEngine on

RewriteRule ^wordpress(.*)$ $1 [R=301,L]

 

【情況三】

 

http://old_domain.com/ >> http://new_domain.com/

 

讓連接到 舊網址 的連結重新定址到 新網址,前提是你必須是舊網址的擁有者

建議讓 舊網址 和 新網址 包持相同的目錄結構

 

把 .htaccess 放到 舊網址 的根目錄,然後要這麼寫:

 

RewriteEngine on

RewriteRule (.*) http://new_domain.com/$1 [R=301,L]

 

【情況四】

 

http://www.your_domain.com/ >> http://your_domain.com/

 

統一你的網址,不要出現 www

由 www.your_domain.com 進入的連結一律重新指向 your_domain.com

 

可以在 .htaccess 中這麼寫:

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.your_domain\.com$ [NC]

RewriteRule ^(.*)$ http://your_domain.com/$1 [R=301,L]