11

我正在尝试在 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 变量中,与上面引用的文档相反。这给我的匹配规则增加了一些复杂性,因为我必须考虑端口号的可选存在。如何获取没有端口号的主机名?

4

1 回答 1

16

不幸的是,我不能告诉你为什么会这样。但我可以告诉你,使用{SERVER_NAME}而不是{HTTP_HOST}为我解决了这个问题。

请参阅:https ://serverfault.com/a/418530/3495

于 2015-05-18T23:58:47.637 回答