我正在尝试在Windows 8.1上使用IIS URL Rewrite 2.0和IIS 8.5。根据{{3}},
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变量中,与上面引用的文档相反。这为我的匹配规则增加了一些复杂性,因为我必须考虑端口号的可选存在。如何在没有端口号的情况下获取主机名?
答案 0 :(得分:7)
不幸的是,我不能告诉你它为什么会发生;但我可以告诉你,使用{SERVER_NAME}
代替{HTTP_HOST}
为我解决了问题。