Dec
Hide Subfolder in Multiple Domains Under One Site
Filed Under (Development, DotNetArabi) by Emad Alashi on 26-12-2011
Tagged Under : iis, multiple domains under one site, url rewrite
After I have moved to WinHost, I was surprised they don’t support multiple domains under one site out of the box; you have to rely on IIS URL Rewrite magic to achieve that.
So as expected, I looked for a solution in their KB and the internet (you don’t reinvent the wheel, remember?) and found couple of posts here, here, here, and here that helped a lot in achieving this.
So I managed to have www.emadashi.com and www.dotnetarabi.com working fine together residing in the folders “emadashi-blog” and “dotnetarabi-root” respectively under the root; they both have same IP address, but upon request a redirection is made to the proper application folder.
Until one day my very good friend Omar Qadan generously shared a link to an episode he found interesting; and the link was as the following:
“www.dotnetarabi.com/dotnetarabi-root/episode.aspx?..etc”
Ops! that’s not right isn’t it! The URL should not include the folder name “dotnetarabi-root”! So I read more about Regular Expressions, read the above mentioned article more thoroughly, and came to the conclusion that for each domain we have we are going to write two rules:
1) Correct and Redirect undesired URL’s:
If IIS receives a request that contains the folder name (e.g “www.dotnetarabi.com/dotnetarabi-root/episode.aspx”), then:
- Omit the folder name from the URL, in our case the URL becomes “www.dotnetarabi.com/episode.aspx”
- Redirect the request again to IIS using the new URL, by sending 301 status prompting the browser to initiate a new request with the new URL
The step above can be achieved by the following:
Figure 1:
- <rule name="UnWantedDirectAccessToSubFolder-DotNetArabi-root" patternSyntax="ECMAScript" stopProcessing="true">
- <match url=".*" />
- <action type="Redirect" url="{C:1}" appendQueryString="true" logRewrittenUrl="false" />
- <conditions>
- <add input="{HTTP_HOST}" pattern="^(www.)?dotnetarabi.com" />
- <add input="{PATH_INFO}" pattern="^[/\\]dotnetarabi_root[/\\](.*)" />
- </conditions>
- </rule>
Then we do the second rule:
2) Guide and Redirect desired URL’s to the right folder:
If IIS receives a request by the URL format we desire, which doesn’t contain the folder name (e.g. “www.dotnetarabi.com/episode.aspx”) then:
- Insert the folder name to the URL to be come “www.dotnetarabi.com/dotnetarabi-root/episode.aspx” in our case
- Rewrite the URL again within IIS using the new URL
Which can be achieved by the following:
Figure 2:
- <rule name="DirectToDotNetArabiRoot" patternSyntax="ECMAScript" stopProcessing="true">
- <match url=".*" />
- <action type="Rewrite" url="dotnetarabi_root/{R:0}" appendQueryString="true" logRewrittenUrl="false" />
- <conditions>
- <add input="{HTTP_HOST}" pattern="^(www.)?dotnetarabi.com" />
- <add input="{PATH_INFO}" pattern="^[/\\]emadalashi_blog[/\\]" negate="true" />
- <add input="{PATH_INFO}" pattern="^[/\\]dotnetarabi_root[/\\]" negate="true" />
- </conditions>
- </rule>
The best way to describe what happens is through this diagram I put together (the start block is the “Browser”):
And that was it, now notice the following:
- The rules should maintain the mentioned order
- Rule 1 uses action type “Redirect”, and Rule 2 uses action type “Rewrite”
- We configure the rules to stop processing any rule after it is executed
- Your key to successful and easy manipulation of the URL is to understand the Back-references in the URL Rewriting, notice the {C:1} and {R:0} usage above, notice as well the brackets () in the URL’s; they are used to capture the back-references, check the “Using back-references in rewrite rules” in the above mentioned article
- To test your rules use Fiddler, IE Developers Tools (Network tab), or Firebug (Net tab), and the great tool IIS provides to test rules which can be accessed by: open IIS –> select site-> URL Rewrite –> Double click rule –> double click condition –> Test pattern:
I hope you found this useful.
Let me do thanks here to www.gliffy.com as well for providing such great service for drawing diagrams online


