简体   繁体   中英

IIS: Use Application Request Routing for URL Rewrite outside of Default Website

I'd like to do a URL rewrite in IIS where subdomain is rewritten. For example:

  • www.mycompany.com/api/v1.0 gets rewritten to api1.mycompany.com
  • www.mycompany.com/api/v2.0 gets rewritten to api2.mycompany.com

Note that I'd like to rewrite and not redirect , in other words, the URL in the browser remains www.mycompany.com/api/v1.0 and www.mycompany.com/api/v2.0 .

Any request that doesn't fit the patterns above should continue to be processed by www.mycompany.com .

My understanding is the URL Rewrite 2.0 module alone isn't enough to make this happen, so I installed Application Request Routing 3.0. Here is the web.config for what I'm trying to do:

<?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>

Once I installed Application Request Routing, I was able to get this to work, but only if www.mycompany.com is the Default Website for IIS. In other words, only if IIS is set up as so:

  • Default Website ( www.mycompany.com )
    • The web.config for this site is shown above
  • api1.mycompany.com
  • api2.mycompany.com

My problem is that www.mycompany.com cannot be the Default Website (the Default Website is reserved by another site on the server). www.mycompany.com is just another web site just like api1.mycompany.com or api2.mycompany.com . Is there any way to get this to work without www.mycompany.com being the Default Website ? Something like this?

  • Default Website (some another non-related website)
  • www.mycompany.com
    • The web.config for this site is as shown above
  • api1.mycompany.com
  • api2.mycompany.com

If you can change the C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config config file, you can put your rewrite configuration inside it.

I test locally and it works under IIS8 and ARR 3.0.

IIS配置

应用请求路由配置

My applicationHost.config file look like this :

<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>

最后结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM