IIS: Use Application Request Routing for a Rewrite URL Outside the Default Website

I would like to rewrite the URL in IIS where the subdomain is rewritten. For instance:

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

Please note that I would like rewrite , 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 does not match the above patterns should continue to be processed by www.mycompany.com .

My understanding is that just one Rewrite 2.0 URL module is not enough for this to happen, so I installed Routing 3.0 applications. Here is a web.config example 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 set up application request routing, I managed to get this to work, but only if www.mycompany.com is the Default Website for IIS. In other words, only if IIS is configured like this:

  • Default Website ( www.mycompany.com )
    • 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 site is reserved by another site on the server). www.mycompany.com is another website similar to api1.mycompany.com or api2.mycompany.com . Is there a way to get this to work without www.mycompany.com being the Default Website ? Something like that?

  • Default website (any other website not associated with it)
  • www.mycompany.com
    • Web.config for this site as shown above
  • api1.mycompany.com
  • api2.mycompany.com
+6
source share
1 answer

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

I am testing locally and working under IIS8 and ARR 3.0.

IIS configuration

Application Request Routing configuration

My applicationHost.config file looks 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> 

Final result

+3
source

Source: https://habr.com/ru/post/986945/


All Articles