HTTP 重定向 <httpRedirect>
将客户端重定向到新位置的原因有多种。 例如,如果公司正在迁移到新网站,则可以将所有请求从旧网站重定向到新网站。 同样,如果在 Web 服务器上部署了新应用程序,则可以将针对旧应用程序的 URL 命名空间(例如 http://www.contoso.com/app_v1.0/)的所有请求重定向到新的应用程序位置(例如 http://www.contoso.com/app_v2.0/)。
在最简单的配置中,只需设置
兼容性
版本
说明
IIS 10.0
向 IIS 10.0 中的
IIS 8.5
IIS 8.0
IIS 7.5
IIS 7.0
IIS 6.0
安装
HTTP 重定向在 IIS 7 及更高版本的默认安装中不可用。 若要安装它,请使用以下步骤。
Windows Server 2012 或 Windows Server 2012 R2
在任务栏上,单击 “服务器管理器”。
在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
在“服务器角色”页,展开“Web 服务器 (IIS)”,展开“Web 服务器”,展开“常见 HTTP 功能”,然后选择“HTTP 重定向”。 单击 “下一步” 。
。
在“选择功能”页上,单击“下一步”。
在“确认安装选择”页上,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
依次展开“Internet Information Services”、“万维网服务”和“通用 HTTP 功能”,然后选择“HTTP 重定向”。
单击“确定”。
单击“关闭” 。
Windows Server 2008 或 Windows Server 2008 R2
在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
在“添加角色服务向导”的“选择角色服务”页,展开“常见 HTTP 功能”,选择“HTTP 重定向”,然后单击“下一步”。
在“确认安装选择”页中,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows Vista 或 Windows 7
在任务栏上,单击“开始”,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
扩展“Internet Information Services”,展开“万维网服务”,然后展开“常见 HTTP 功能”。
选择“HTTP 重定向”,然后单击“确定”。
操作方式
IIS 7 中没有用于添加通配符 HTTP 重定向的用户界面。 有关如何以编程方式将
如何将 HTTP 重定向规则添加到网站或应用程序
打开 Internet Information Services (IIS) 管理器:
如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:
在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows 8 或 Windows 8.1:
按住 Windows 键,按字母 X,然后单击“控制面板”。
单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:
在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”。
如果使用的是 Windows Vista 或 Windows 7:
在任务栏上,单击“开始”,然后单击“控制面板”。
双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
在“连接”窗格中,展开服务器名称,展开“站点”,然后导航到要为其配置自定义错误页面的网站或应用程序。
在“主页”窗格中,双击“HTTP 重定向”。
在“HTTP 重定向”窗格中,选中用于重定向请求并输入目标 URL 的框。
可以选择指定以下任一选项:
将重定向目标配置为输入的确切目标。
将重定向目标配置为限制为目标 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 永久重定向
完成上述所有更改后,在“任务”窗格中单击“应用”。
配置
特性
属性
说明
childOnly
可选布尔属性。 指定是否应将目标值(包含要重定向的请求)添加到文件名开头。 例如,如果 childOnly 设置为 true,并且目标值配置为 http://marketing.contoso.com/,则会将对 http://contoso.com/default.htm 的请求重定向到 http://marketing.contoso.com/default.htm。 默认值为 false。
destination
可选的字符串属性。 指定要将客户端重定向到的 URL 或虚拟路径。
enabled
可选布尔属性。 指定是启用 (true) 还是禁用 (false) 重定向。 默认值为 false。
exactDestination
可选布尔属性。 指明目标值应被视为绝对目标位置,而不是相对位置。 默认值为 false。
httpResponseStatus
可选的枚举属性。 指定重定向的类型。 httpResponseStatus 属性的值可以是下列其中一个。 默认为 Found。 值 说明 Found 返回 302 状态代码,该状态码命令客户端向目标属性中指定的位置发出新请求。 数值为 302。 Permanent 返回 301 状态代码,该状态码告知客户端所请求资源的位置已永久更改。 数值为 301。 Temporary 返回 307 状态代码,以防止客户端在浏览器发出 HTTP POST 请求时丢失数据。 数值为 307。 PermRedirect 返回 308 状态代码,该代码通知客户端所请求资源的位置已永久更改。 数值为 308。
子元素
元素
说明
add
可选元素。 将通配符重定向规则添加到重定向规则集合。
clear
可选元素。 从重定向规则集合中移除对通配符重定向规则的所有引用。
remove
可选元素。 从重定向规则集合中移除对通配符重定向规则的引用。
配置示例
安装 HTTP 重定向角色服务时,会在 IIS 7 的根 ApplicationHost.config 文件中配置以下默认
以下配置示例将启用重定向,并配置客户端重定向到的目标 URL。
以下配置示例将添加一个通配符重定向条目,用于将 PHP 文件的所有请求重定向到您的网站主页。
注意
在以下情况下,此示例非常有用:您已从网站中移除所有基于 ASP 的应用程序,并且您希望将旧应用程序的客户端请求重定向到网站的根目录,而不是让用户收到“HTTP 404 找不到”响应。
代码示例
下面的代码示例将默认网站配置为使用 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()