使用IIS URL重写更改主机头

使用IIS URL重写更改主机头,iis,url-rewriting,Iis,Url Rewriting,我的Web服务器上有请求 http://www.example.com/blog 我想把它重写到另一台正在监听的服务器上 http://博客.example.com 以下是我的重写规则: <rule name="blogredirect1" stopProcessing="true"> <match url="^blog(.*)" /> <action type="Rewrite" url="http://blog.examp

我的Web服务器上有请求

http://www.example.com/blog

我想把它重写到另一台正在监听的服务器上

http://博客.example.com

以下是我的重写规则:

<rule name="blogredirect1" stopProcessing="true">
          <match url="^blog(.*)" />
          <action type="Rewrite" url="http://blog.example.com{R:1}"  />
</rule>

最终发生的是重写将请求发送到位于blog.mysite.com的第二个服务器IP,但请求头的主机仍然是www.mysite.com


如何确保重定向请求主机设置为blog.mysite.com(在重定向规则中设置)

我可以知道您是如何检查主机头的吗

建议检查{HTTP_HOST}变量,而不是直接查看HTTP头。因为如果您查看请求头,您将始终看到www.mysite.com

您可以从后端服务器blog.mysite.com获取{HTTP_HOST}请求变量

但是,如果您的意思是blog.mysite.com中的页面也将{HTTP_HOST}显示为www.mysite.com。那么请检查您是否已将system.webServer/proxy/preserveHostHeader设置为true

顺便说一下,IIS支持手动重写HTTP_主机,您可以这样修改规则:

               <rule name="blogredirect1" stopProcessing="true">
          <match url="^blog(.*)" />
          <action type="Rewrite" url="http://blog.example.com{R:1}" />
                    <serverVariables>
                        <set name="HTTP_HOST" value="blog.example.com" />
                    </serverVariables>
</rule>

您还可以在applicationhost.config
部分中进行设置

 <location path="Default Web Site">
        <system.webServer>
            <rewrite>
                <allowedServerVariables>
                    <add name="HTTP_HOST" />
                </allowedServerVariables>
            </rewrite>
        </system.webServer>
    </location>

谢谢,你的答案很有效。我可以更换主机。:)