Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 696
Johnny Oshika Avatar asked May 11 '15 06:05

Johnny Oshika


People also ask

What is the difference between routing and URL rewriting?

URL rewriting is focused on mapping one URL (new url) to another URL (old url) while routing is focused on mapping a URL to a resource. Actually, URL rewriting rewrites your old url to new one while routing never rewrite your old url to new one but it map to the original route.

How does IIS URL Rewrite work?

The concept of URL rewriting is simple. When a client sends a request to the Web server for a particular URL, the URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.

How to enable application request routing for IIS on Windows Server?

The first step is to install the add-on module for Application Request Routing for IIS. With Windows Server 2012 R2 or later versions of Windows Server 2016 and 2018, you can use the Microsoft Web Platform Installer 5.1 (WebPI) to download and install the URL Rewrite Module. Just search for ‘URL Rewrite’ in the search options and click ‘Add’.

Should I use IIs URL Rewrite or ASP NET routing?

If your Web application is built by using anything except ASP.NET, use the IIS URL Rewrite module. Otherwise, the rules are: If you are developing a new ASP.NET Web application that uses either ASP.NET MVC or ASP.NET Dynamic Data technologies, use ASP.NET routing.

What is url rewrite module and Application Request Routing?

By using URL Rewrite Module and Application Request Routing you can implement complex and flexible load balancing and reverse proxy configurations. A very common reverse proxy scenario is to make available several internal web applications over the Internet.

What is Arr in IIS rewrite?

You may also require enabling Application Request Routing (ARR) with the Rewrite module. IIS Application Request Routing offers administrators the ability to create powerful routing rules based on the URL, HTTP headers, and server variables to determine the most appropriate Web application server for each request.


1 Answers

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 configuration

Application Request Routing configuration

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>

Final result

like image 130
Cyril Durand Avatar answered Oct 06 '22 07:10

Cyril Durand