關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 實現 301 轉向的幾個方法

    Posted on April 13th, 2006 Sam Tang 1 comment

    如果需要做網址轉向,一般上在 php 用 header() 實現的轉向是 302:

    header("Location: http://www.newaddress.com/");

    301 及 302 同樣可以做到網址的轉向,但分別在於 301 是永久的轉向,而 302 是暫時性質的轉向。這個分別對於使用者來說是沒有分別的,但對於 search engine 就有分別了。Google 推薦網站管理員當轉換網址時用 301 轉向。而以下分別是透過 .htaccess、PHP 及 ASP 實現 301 轉向的方法,而假設新 domain 是 newdomain.com:

    .htaccess
    在網站的根目錄建立一個 .htaccess 檔案,並加入以下內容:

    RewriteEngine On
    RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]

    PHP
    在 index.php 的最頂加入以下幾行:

    PHP:
    1. <?php
    2. header("HTTP/1.1 301 Moved Permanently");
    3. header("Location: http://www.newdomain.com/");
    4. exit();
    5. ?>

    ASP
    在 index.asp 或 default.asp 的最頂加入以下幾行:

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


     

    One response to “實現 301 轉向的幾個方法”

    1. [...] 實現 301 轉向的幾個方法 (tags: Tech WebService) [...]

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word