You are here: » Windows Server » RewriteProxy with .htaccess in IIS

RewriteProxy with .htaccess in IIS

Rewrite and proxy HTTP requests in IIS using a .htaccess

In my case scenario, I had to proxy requests in IIS, because a website was moved from web server A to B, and the DNS wasn’t updated yet. All HTTP requests for the moved website are handled in IIS’ Default Web Site; that’s the wildcard host, and the original host no longer existed there. We needed to match our website and proxy those requests to the new IIS web server. This can either be done using a proxy with URL Rewrite, IIS Application Request Routing (ARR), or a .htaccess file handled by Helicon Ape.

Web.config prerequisites for .htaccess proxy

The .htaccess RewriteProxy directive gives us the opportunity to rewrite requests from domain/web server A to web server B. Of course, the host must be known on web server B, since we preserve the hostname.

Given the scenario, we chose to RewriteProxy all requests using a .htaccess file.

Depending on how you installed Helicon Ape, you may -or may not- have to create a specific .apehandler. If you didn’t installed Helicon Ape automatically using an .msi file, but manually, you probably need this. Otherwite Ape can’t proxy requests through an .htaccess file.

IIS web.config configuration for Helicon Ape proxy

As said, you need to configure the special .apehandler in IIS to start proxying requests. Add to your web.config file (if required):

<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <!-- The Helicon.Ape version number must match your installed Ape version or IIS will return an error! --> <add name="Helicon.Ape Handler" path="*.apehandler" verb="*" type="Helicon.Ape.Handler, Helicon.Ape, Version=, Culture=neutral, PublicKeyToken=95bfbfd1a38437eb" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" /> </handlers> </system.webServer> </configuration>

RewriteProxy .htaccess script

Our .htaccess file we need (explanation is in the comments):

# Enable the rewrite engine RewriteEngine On # Proxy requests for and to # a different web server hosting example.het, by its IP address and # preserving its Host: HTTP header. This is useful for when you just # moved a website and DNS hasn't been refreshed yet. RewriteCond %{HTTP_HOST} ^www\$ [OR] RewriteCond %{HTTP_HOST} ^$ RewriteCond %{REQUEST_URI} (.+) # %1 matches your first matched value, thus (.+) in this case, # or "www" when I use: # RewriteCond %{HTTP_HOST} ^(www\.)$ # Adjust %1 to %2 or %3 where appropriate. RewriteProxy .? [H,L]

Another great RewriteProxy option is:

# Enable the rewrite engine RewriteEngine On # Proxies requests to pull the content from site B for display on site A # # For example proxies to # (note the different TLD) # RewriteProxy ^(.*)$1 [H,L]

The [H] flag insures the Host: HTTP header is preserved. This means the hostname, or website domain name, needs to exist on other web server (B). When you omit the [H] flag, the requested resource is pulled from the remote destination server and shown on the requested web site:

RewriteProxy ^images/(.*)$1

Another example, pulled from an old post:

# Enable the RewriteEngine. RewriteEngine On # Rewrite hostheaders and through to # another web server using a RewriteProxy request. # # Change with your domain name and # with the IP address of web server B. # RewriteCond %{HTTP:Host} ^(?:www.)?$ RewriteCond %{REQUEST_URI} (.+) RewriteProxy .? [H]


These are all very basic forms of proxying on IIS, there is absolutely no need for extensive mod_proxy configurations. All we need is mod_rewrite‘s RewriteProxy.

This allows you to create and maintain useful proxy mechanisms in IIS, where IIS Application Request Routing (ARR) isn’t always available. Helicon Ape‘s mod_rewrite can be a powerful and useful proxy mechanism.

Leave a Reply

Your email address will not be published. Required fields are marked *