在 IIS URL Rewrite 2.0 中,为啥 HTTP_HOST 包含端口号?

Posted

技术标签:

【中文标题】在 IIS URL Rewrite 2.0 中,为啥 HTTP_HOST 包含端口号?【英文标题】:In IIS URL Rewrite 2.0, why does HTTP_HOST include the port number?在 IIS URL Rewrite 2.0 中,为什么 HTTP_HOST 包含端口号? 【发布时间】:2013-10-15 22:28:12 【问题描述】:

我正在尝试在 Windows 8.1 上将 IIS URL Rewrite 2.0 与 IIS 8.5 一起使用。根据Accessing URL Parts from a Rewrite Rule,

For an HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>

• The <path> is matched against the pattern of the rule.
• The <querystring> is available in the server variable called QUERY_STRING and can be accessed by using a condition within a rule.
• The <host> is available in the server variable HTTP_HOST and can be accessed by using a condition within a rule.
• The <port> is available in the server variable SERVER_PORT and can be accessed by using a condition within a rule.
• Server variables SERVER_PORT_SECURE and HTTPS can be used to determine if a secure connection was used. These server variables can be accessed by using a condition within a rule.
• The server variable REQUEST_URI can be used to access the entire requested URL path, including the query string.

为了测试,这是我使用的规则:

<rule name="Test" stopProcessing="true">
  <action type="Redirect" url="http://localhost/?HTTP_HOST" redirectType="Temporary" />
</rule>

然后我使用 Fiddler 中的 Composer 选项卡创建以下请求:

http://localhost.localdomain:65352/

IIS 响应的

HTTP/1.1 307 Moved Temporarily
Location: http://localhost/?localhost.localdomain:65352

从这里我们可以看到端口号包含在 HTTP_HOST 变量中,与上面引用的文档相反。这给我的匹配规则增加了一些复杂性,因为我必须考虑端口号的可选存在。如何获取没有端口号的主机名?

【问题讨论】:

仅供参考:我创建了一个问题here。 【参考方案1】:

很遗憾,我无法告诉你为什么会这样;但我可以告诉你,使用 SERVER_NAME 而不是 HTTP_HOST 为我解决了这个问题。

见:https://serverfault.com/a/418530/3495

【讨论】:

以上是关于在 IIS URL Rewrite 2.0 中,为啥 HTTP_HOST 包含端口号?的主要内容,如果未能解决你的问题,请参考以下文章

IIS 10.0 无法安装 URL rewrite重写模块 2.0

IIS Url Rewrite Module 导致“无法在网络服务器上开始调试”

IIS重写2.0 IIS伪静态 下载地址

减少链接 URL 重定向 [IIS/Regex/ReWrite]

IIS8如何安装和使用URL重写工具-URL Rewrite

IIS配置页面重写(配合插件url-rewrite2去除页面后缀名)