This article addresses a practical solution for managing multiple static websites in IIS without creating separate website instances. The approach leverages URL rewriting to convert hostnames into subfolders.
Key Concept
The technique maps incoming requests based on their hostname to corresponding subdirectories. For example, http://www.mydomain.com/favicon.ico redirects to c:\inetpub\wwwroot\mydomain.com\favicon.ico.
Technical Implementation
The solution uses two components:
Regular Expression Pattern
^(www\.)?(.+?)(:[0-9]+)?$ This extracts the domain name while handling optional "www" prefixes and port numbers.
Rewrite Rule
The captured hostname (C:2) prefixes the requested path (R:0), creating the destination: {C:2}/{R:0}
Configuration
The web.config contains the complete IIS rewrite rule setup with condition matching and rewrite action specifications.
Update (2013-04-11)
A subsequent update corrected a regex flaw: the regular expression did not work when no port was specified, causing requests to bypass rewriting entirely. The fix addressed cases like direct domain access without port notation.