我想在IIS中重写子域,并重写子域。例如:
www.mycompany.com/api/v1.0
被重写为api1.mycompany.com
www.mycompany.com/api/v2.0
被重写为api2.mycompany.com
请注意,我希望rewrite
而不是redirect
,换句话说,浏览器中的网址仍为www.mycompany.com/api/v1.0
和www.mycompany.com/api/v2.0
。
任何不符合上述模式的请求都应继续由www.mycompany.com
处理。
我的理解是仅凭URL Rewrite 2.0模块不足以实现这一点,所以我安装了Application Request Routing 3.0。这是我尝试做的web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<rewrite>
<rules>
<rule name="API v1.0" stopProcessing="true">
<match url="^api/v1.0/(.*)$" />
<action type="Rewrite" url="http://api1.mycompany.com/{R:1}" />
</rule>
<rule name="API v2.0" stopProcessing="true">
<match url="^api/v2.0/(.*)$" />
<action type="Rewrite" url="http://api2.mycompany.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
一旦我安装了应用程序请求路由,我就可以使用它,但前提是www.mycompany.com
是IIS的Default Website
。换句话说,仅当IIS设置为如此:
www.mycompany.com
)
api1.mycompany.com
api2.mycompany.com
我的问题是www.mycompany.com
不能是Default Website
(默认网站由服务器上的其他网站保留)。 www.mycompany.com
只是另一个网站,就像api1.mycompany.com
或api2.mycompany.com
一样。如果www.mycompany.com
不是Default Website
,有没有办法让这个工作?像这样的东西?
www.mycompany.com
api1.mycompany.com
api2.mycompany.com
答案 0 :(得分:4)
如果您可以更改C:\Windows\System32\inetsrv\config\applicationHost.config
配置文件,则可以将重写配置放入其中。
我在本地测试,它在IIS8和ARR 3.0下工作。
我的applicationHost.config文件如下所示:
<system.webServer>
<rewrite>
<rules>
<rule name="API v1.0" stopProcessing="true">
<match url="^api/v1.0/(.*)$" />
<action type="Rewrite" url="http://api1.company.com/{R:1}" />
</rule>
<rule name="API v2.0" stopProcessing="true">
<match url="^api/v2.0/(.*)$" />
<action type="Rewrite" url="http://api2.company.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
<!-- ... --->
<webFarms>
<applicationRequestRouting>
<hostAffinityProviderList>
<add name="Microsoft.Web.Arr.HostNameRoundRobin" />
<add name="Microsoft.Web.Arr.HostNameMemory" />
</hostAffinityProviderList>
</applicationRequestRouting>
</webFarms>
<!-- ... --->
<system.applicationHost>
<sites>
<site name="Default Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:" />
</bindings>
</site>
<site name="company.com" id="2">
<application path="/" applicationPool="company.com">
<virtualDirectory path="/" physicalPath="C:\tmp\company.com\www" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:www.company.com" />
</bindings>
</site>
<site name="api1.company.com" id="3">
<application path="/" applicationPool="api1.company.com">
<virtualDirectory path="/" physicalPath="C:\tmp\company.com\api1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:api1.company.com" />
</bindings>
</site>
<site name="api2.company.com" id="4">
<application path="/" applicationPool="api2.company.com">
<virtualDirectory path="/" physicalPath="C:\tmp\company.com\api2" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:api2.company.com" />
</bindings>
</site>
</sites>
</system.applicationHost>