HTTP 重新導向 <HTTPRedirect>

元素 <httpRedirect> 會設定 Internet Information Services (IIS) 7 的設定,以將用戶端要求重新導向至新的位置。

您可能會想要將用戶端重新導向至新位置的原因有數個。 例如,如果您的公司正在移轉至新的網站,您可以將所有要求從舊網站重新導向至新的網站。 同樣地,如果您在 Web 伺服器上部署新的應用程式,您可以將舊應用程式的 URL 命名空間的所有要求重新導向至 (例如, http://www.contoso.com/app_v1.0/) 至新的應用程式位置 (,例如 http://www.contoso.com/app_v2.0/ ,) 。

在最簡單的設定中,您只需要設定專案的 已啟用目的地 屬性 <httpRedirect> ,才能將用戶端重新導向至新的位置。 不過, 如 exactDestinationHTTPResponseStatus 屬性等其他元素,可讓您分別指定 IIS 7 是否會傳回目的地 URL,以及要傳回至 Web 用戶端的 HTTP 回應碼。

相容性

版本 備註
IIS 10.0 已將額外的 HTTP 回應狀態新增至 <httpRedirect> IIS 10.0 中的專案。
IIS 8.5 <httpRedirect> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <httpRedirect>
IIS 7.5 <httpRedirect> 在 IIS 7.5 中修改專案。
IIS 7.0 專案 <httpRedirect> 是在 IIS 7.0 中引進的。
IIS 6.0 元素 <httpRedirect> 會取代 IIS 6.0 HttpRedirect 中繼基底屬性。

設定

HTTP 重新導向不適用於 IIS 7 和更新版本的預設安裝。 若要安裝,請使用下列步驟。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在工作列上,按一下 [伺服器管理員]
  2. 伺服器管理員 中,按兩下 [管理] 功能表,然後按兩下 [新增角色和功能]。
  3. 在 [ 新增角色和功能 精靈] 中,按 [下一步]。 選取安裝類型,然後按 [ 下一步]。 選取目的地伺服器,然後按 [ 下一步]。
  4. 在 [ 伺服器角色] 頁面上,依序展開 [Web 伺服器] ([IIS) ]、[ Web 伺服器]、[ 一般 HTTP 功能],然後選取 [ HTTP 重新導向]。 按一下 [下一步]。
    [伺服器角色] 頁面的螢幕快照。已選取 H T T P 重新導向並醒目提示。 .
  5. 在 [選取功能] 頁面上,按 [下一步]
  6. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  7. 在 [結果] 頁面上,按一下 [關閉]

Windows 8 或 Windows 8.1

  1. 在 [開始] 畫面上,將指標全部移至左下角,以滑鼠右鍵按兩下 [開始] 按鈕,然後按兩下 [控制台]。
  2. 控制台 中,按兩下 [程式和功能],然後按兩下 [開啟或關閉 Windows 功能]。
  3. 序展開 [Internet Information Services]、[ 萬維網服務]、[ 通用 HTTP 功能],然後選取 [ HTTP 重新導向]。
    Internet Information Services 瀏覽樹狀結構的螢幕快照。[萬維網服務] 選項已展開。已展開一般 H T T P 功能,並選取 [H T T P 重新導向]。
  4. 按一下 [確定]。
  5. 按一下 [關閉] 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任務欄上,按兩下 [開始],指向 [系統管理工具],然後按兩下 [伺服器管理員]。
  2. [伺服器管理員 階層] 窗格中,展開 [角色],然後按兩下 [Web 伺服器] (IIS)
  3. [Web 伺服器 (IIS) ] 窗格中,捲動至 [ 角色服務 ] 區段,然後按兩下 [ 新增角色服務]。
  4. 在 [新增角色服務精靈] 的 [選取角色服務] 頁面上,展開 [通用 Http 功能],選取 [HTTP 重新導向],然後按 [下一步]。
    角色服務區段的螢幕快照。已選取 H T T P 重新導向並醒目提示。
  5. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  6. 在 [結果] 頁面上,按一下 [關閉]

Windows Vista 或 Windows 7

  1. 在任務欄上,按兩下 [開始],然後按兩下 [控制台]。
  2. 控制台 中,按兩下 [程式和功能],然後按兩下 [開啟或關閉 Windows 功能]。
  3. 展開 [Internet Information Services]、[ 萬維網服務]、[ 通用 Http 功能]。
  4. 選取 [HTTP 重新導向],然後按兩下 [ 確定]。
    [程式和功能] 瀏覽樹狀結構的螢幕快照。已選取 H T T P 重新導向並醒目提示。

作法

沒有用於新增 IIS 7 通配符 HTTP 重新導向的使用者介面。 For examples of how to add <add> elements to the <httpRedirect> element programmatically, see the Code Samples section of this document.

如何將 HTTP 重新導向規則新增至網站或應用程式

  1. (IIS) 管理員開啟 Internet Information Services

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任務欄上,按兩下 [伺服器管理員]、[工具],然後按兩下 [Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住 Windows 鍵,按字母 X,然後按兩下 [控制台]。
      • 單擊 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任務欄上,按兩下 [ 開始],指向 [ 系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在任務欄上,按兩下 [開始],然後按兩下 [控制台]。
      • 按兩下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
  2. 在 [ 連線 ] 窗格中,展開伺服器名稱、[ 網站],然後流覽至您要設定自定義錯誤頁面的網站或應用程式。

  3. 在 [ 首頁] 窗格中,按兩下 [HTTP 重新導向]。
    [首頁] 窗格的螢幕快照。H T T P 重新導向圖示已醒目提示並選取。

  4. 在 [ HTTP 重新導向 ] 窗格中,核取方塊以重新導向要求並輸入目的地 URL。
    H T T P 重新導向窗格的螢幕快照。核取 [將要求重新導向至此目的地] 旁的方塊。

  5. 您可以選擇性指定下列任一選項:

    • 將重新導向目的地設定為輸入的確切目的地。

    • 將重新導向目的地設定為僅限於目的地 URL 的根資料夾,而不是子資料夾。

    • 設定 HTTP 狀態代碼,可以是下列其中一個選項:

      • 301 永久
      • 302 已找到
      • 307 暫存
      • 308 永久重新導向

      注意

      IIS 7 會分別針對上述每個選項傳回下列實際的 HTTP 回應狀態:

      • HTTP/1.1 301 已永久移動
      • HTTP/1.1 302 重新導向
      • HTTP/1.1 307 重新導向
      • HTTP 1.1 308 已永久重新導向
  6. 當您完成上述所有變更之後,請按兩下 [工作] 窗格中的 [套用]。

設定

屬性

屬性 Description
childOnly 選擇性的 Boolean 屬性。

指定目的地值是否應該加入至檔名開頭,其中包含要重新導向的要求。 例如,如果 childOnly 設定為 true,且目的地值設定為 http://marketing.contoso.com/,則會將 的要求 http://contoso.com/default.htm 重新導向至 http://marketing.contoso.com/default.htm

預設值是 false
destination 選擇性字串屬性。

指定要重新導向用戶端的 URL 或虛擬路徑。
enabled 選擇性的 Boolean 屬性。

指定是否 (true 啟用重新導向,) 或停用 (false) 。

預設值是 false
exactDestination 選擇性的 Boolean 屬性。

指定目的地值應該視為絕對目標位置,而不是相對位置。

預設值是 false
httpResponseStatus 選擇性列舉屬性。

指定重新導向的類型。

HTTPResponseStatus 屬性可以是下列其中一個可能的值。 預設值為 Found
Description
Found 傳回 302 狀態代碼,告知用戶端對目的地屬性中指定的位置發出新要求。

數值為 302
Permanent 傳回 301 狀態代碼,通知用戶端要求之資源的位置已永久變更。

數值為 301
Temporary 傳回 307 狀態代碼,這可防止用戶端在瀏覽器發出 HTTP POST 要求時遺失數據。

數值為 307
PermRedirect 傳回 308 狀態代碼,通知用戶端要求的資源位置已永久變更。

數值為 308

子元素

元素 Description
add 選擇性項目。

將通配符重新導向規則新增至重新導向規則的集合。
clear 選擇性項目。

從重新導向規則集合中移除通配符重新導向規則的所有參考。
remove 選擇性項目。

從重新導向規則的集合中移除通配符重新導向規則的參考。

組態範例

安裝 HTTP 重新導向角色服務時,下列預設 <httpRedirect> 元素會在 IIS 7 的根 ApplicationHost.config 檔案中設定。 除非您使用 元素,否則此組態區段會繼承預設組 <clear> 態設定。

<system.webServer>
   <httpRedirect enabled="false" />
</system.webServer>

下列設定範例會啟用重新導向,並設定用戶端重新導向的目標 URL。

<system.webServer>
   <httpRedirect enabled="true" destination="http://www.contoso.com/" />
</system.webServer>

下列組態範例會新增通配符重新導向專案,將 PHP 檔案的所有要求重新導向至網站的首頁。

注意

如果您已從您的網站移除所有 ASP 型應用程式,而且您想要將舊應用程式的用戶端要求重新導向至網站的根目錄,而不是接收 HTTP 404 找不到 回應,則此範例很有用。

<configuration>
   <system.webServer>
      <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
         <add wildcard="*.php" destination="/default.htm" />
      </httpRedirect>
   </system.webServer>
</configuration>

範例程式碼

下列程式代碼範例會將預設網站設定為使用 HTTP 302 狀態代碼將所有要求重新導向至 http://www.contoso.com

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /enabled:"True"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /destination:"http://www.contoso.com"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /exactDestination:"False"
appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /httpResponseStatus:"Found"

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetWebConfiguration("Default Web Site");

         ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
         httpRedirectSection["enabled"] = true;
         httpRedirectSection["destination"] = @"http://www.contoso.com";
         httpRedirectSection["exactDestination"] = false;
         httpRedirectSection["httpResponseStatus"] = @"Found";

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")

      Dim httpRedirectSection As ConfigurationSection = config.GetSection("system.webServer/httpRedirect")
      httpRedirectSection("enabled") = True
      httpRedirectSection("destination") = "http://www.contoso.com"
      httpRedirectSection("exactDestination") = False
      httpRedirectSection("httpResponseStatus") = "Found"

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site");
httpRedirectSection.Properties.Item("enabled").Value = true;
httpRedirectSection.Properties.Item("destination").Value = "http://www.contoso.com";
httpRedirectSection.Properties.Item("exactDestination").Value = false;
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found";

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site")
httpRedirectSection.Properties.Item("enabled").Value = True
httpRedirectSection.Properties.Item("destination").Value = "http://www.contoso.com"
httpRedirectSection.Properties.Item("exactDestination").Value = False
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found"

adminManager.CommitChanges()

下列程式代碼範例會新增通配符重新導向專案,將ASP檔案的所有要求重新導向至網站的首頁。

注意

如果您已從您的網站移除所有 ASP 型應用程式,而且您想要將舊應用程式的用戶端要求重新導向至網站的根目錄,而不是接收 HTTP 404 找不到 回應,則此範例很有用。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /enabled:"True" /exactDestination:"True" /httpResponseStatus:"Found"

appcmd.exe set config "Default Web Site" -section:system.webServer/httpRedirect /+"[wildcard='*.asp',destination='/default.htm']"

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using(ServerManager serverManager = new ServerManager()) { 
         Configuration config = serverManager.GetWebConfiguration("Default Web Site");

         ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
         httpRedirectSection["enabled"] = true;
         httpRedirectSection["exactDestination"] = true;
         httpRedirectSection["httpResponseStatus"] = @"Found";

         ConfigurationElementCollection httpRedirectCollection = httpRedirectSection.GetCollection();
         ConfigurationElement addElement = httpRedirectCollection.CreateElement("add");
         addElement["wildcard"] = @"*.asp";
         addElement["destination"] = @"/default.htm";
         httpRedirectCollection.Add(addElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")

      Dim httpRedirectSection As ConfigurationSection = config.GetSection("system.webServer/httpRedirect")
      httpRedirectSection("enabled") = True
      httpRedirectSection("exactDestination") = True
      httpRedirectSection("httpResponseStatus") = "Found"

      Dim httpRedirectCollection As ConfigurationElementCollection = httpRedirectSection.GetCollection
      Dim addElement As ConfigurationElement = httpRedirectCollection.CreateElement("add")
      addElement("wildcard") = "*.asp"
      addElement("destination") = "/default.htm"
      httpRedirectCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site");
httpRedirectSection.Properties.Item("enabled").Value = true;
httpRedirectSection.Properties.Item("exactDestination").Value = true;
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found";

var httpRedirectCollection = httpRedirectSection.Collection;
var addElement = httpRedirectCollection.CreateNewElement("add");
addElement.Properties.Item("wildcard").Value = "*.asp";
addElement.Properties.Item("destination").Value = "/default.htm";
httpRedirectCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set httpRedirectSection = adminManager.GetAdminSection("system.webServer/httpRedirect", "MACHINE/WEBROOT/APPHOST/Default Web Site")
httpRedirectSection.Properties.Item("enabled").Value = True
httpRedirectSection.Properties.Item("exactDestination").Value = True
httpRedirectSection.Properties.Item("httpResponseStatus").Value = "Found"

Set httpRedirectCollection = httpRedirectSection.Collection
Set addElement = httpRedirectCollection.CreateNewElement("add")
addElement.Properties.Item("wildcard").Value = "*.asp"
addElement.Properties.Item("destination").Value = "/default.htm"
httpRedirectCollection.AddElement(addElement)

adminManager.CommitChanges()