IIS url rewrite hostname to subfolder

Say that you have several (thousand) static websites and you do not want to manage a seperate website within IIS. What can we do? Well, we can use url rewriting to rewrite the url and use the hostname to specify a subfolder.

Some examples with the rewrite destination:

Two different domains: => c:\inetpub\wwwroot\\favicon.ico => c:\inetpub\wwwroot\\favicon.ico

Same domain with and without www subdomain: => c:\inetpub\wwwroot\\favicon.ico => c:\inetpub\wwwroot\\favicon.ico

Different ports and protocols: => c:\inetpub\wwwroot\\favicon.ico => c:\inetpub\wwwroot\\favicon.ico

Implementation behavior

The basic idea is to get the domain part from the host name and prefix this to the path. The hostname is stored in the {HTTP_HOST} variable. The problem is that this also has the port appended to it. It took me a while before I noticed that the port number was specified by IIS but after that finishing the rewrite action was easy.

  1. Regular expression to retrieve the hostname without the port number:


  2. Rewrite rule that prefixes the captured path (R:0) with the captured hostname (C:2)



Here is the actual web.config containing the url rewrite configuration. Hope this helps!

<?xml version="1.0" encoding="UTF-8"?>
        <rule name="hostname2subfolder">
          <!--See: -->
          <match url=".*" />
          <conditions trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.+?)(:[0-9]+)?$" />
          <action type="Rewrite" url="{C:2}/{R:0}" logRewrittenUrl="true" />

Update 2013-04-11

The regular expression did not work when no port was specified f.e. failed to match which results in url rewrite to not rewrite at all. I only tested the previous version through a load-balancer which redirected traffic to non default ports.

comments powered by Disqus
← Older Newer →