You are here: Sysadmins of the North » 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. Otherwise 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. Use appcmd.exe to configure the *.apehandler IIS Managed Handler:

appcmd.exe set config /section:system.webServer/handlers "/+[name=`'Helicon.Ape Handler`',path=`'*.apehandler`',verb=`'*`',type=`'Helicon.Ape.Handler, Helicon.Ape, Version=, Culture=neutral, PublicKeyToken=95bfbfd1a38437eb`',resourceType=`'Unspecified`',preCondition=`'integratedMode`']"Code language: PowerShell (powershell)

Or manually add to your web.config file (if required):

<?xml version="1.0" encoding="UTF-8"?>
          <!-- The Helicon.Ape version number must match your installed
               Ape version or IIS will return an error! -->
          <add name="Helicon.Ape Handler"
            type="Helicon.Ape.Handler, Helicon.Ape, Version=,
            Culture=neutral, PublicKeyToken=95bfbfd1a38437eb"
            preCondition="integratedMode" />
</configuration>Code language: HTML, XML (xml)

RewriteProxy .htaccess example

To use RewriteProxy, use this in your .htaccess file (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]Code language: Apache (apache)

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]Code language: Apache (apache)

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/(.*)$1Code language: Apache (apache)

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]Code language: Apache (apache)

Conclusion proxying requests using Helicon Ape on IIS

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.

Show Your Support

donate with Paypal

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️

Leave a Comment

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

Scroll to Top