重写 URL 以指向 IIS 中的另一个站点

问题描述

我在 IIS 8.5 中设置了两个站点,为了简单起见,我们将其称为 Test 1Test 2

测试 1 可在 test1.mydomain.com 访问,测试 2 可在 test2.mydomain.com 访问,但我希望它也可用在test1.mydomain.com/app

这不应该是重定向,而是重写 - 当用户查看 Test 2 站点时,他们应该在浏览器地址栏中看到 URL test1.mydomain.com/app,以及所有路径应该使用它作为基本网址,例如test1.mydomain.com/app/page1

我希望使用 URL 重写模块会很简单,但似乎无法重写域,只能重写路径:

    <rule name="test2" stopProcessing="true">
        <match url="^app(/.*)?$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
        <action type="Rewrite" url="test2.mydomain.com{R:1}" appendQueryString="false" />
    </rule>

将此添加Test 1 的 web.config 会导致 404 错误,我假设是因为它将 URL 重写为 test1.mydomain.com/test2.mydomain.com

如何重写 URL 以指向测试 2 站点

解决方法

使用URL Rewrite无法改写域名,URL Rewrite只能改写域名后面的内容。

可以使用ARR中的反向代理来实现这个功能:

enter image description here

以下是配置反向代理的步骤:

  1. 如果您还没有安装ARR,您需要先安装它。

  2. 安装成功后双击ARR:

enter image description here

  1. 设置反向代理:

enter image description here

  1. 启用反向代理:

enter image description here

  1. 最后,在您的站点中配置 URL 重写:

enter image description here

有关“具有 URL 重写 v2 和应用程序请求路由的反向代理”的更多信息,您可以参考此 link

更新:

您可以启用 FRT 来查看重写后的 URL 是什么。

这是一个演示:

enter image description here

enter image description here

enter image description here

你可以尝试使用这个规则:

<rule name="test2" stopProcessing="true">
        <match url="^app(/.*)?$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
        <action type="Rewrite" url="http://test2.mydomain.com{R:1}" appendQueryString="false" />
    </rule>

使用此操作:

<action type="Rewrite" url="http://test2.mydomain.com{R:1}" appendQueryString="false" />

不要忘记添加 http://。