How To Rewrite A Sub Domain To A Directory In IIS

When I signed up with my current web host the deal was limited to six domains and unlimited websites; a restriction I figured I could live within as a lot of what I intended to do would be in sub-domains and the unlimited websites allowed me to have each sub-domain in an individual website.

However, since I signed up they have, without giving any form of notification, changed the deal so it is now unlimited domains and only six websites. I’m currently in a position that I have five domains and twelve websites without the ability to create another website; the control panel shows me as having 12 of 6 websites. The only positive is that they have not, as yet, required me to trim down the number of websites I have.

I therefore spent a little time yesterday looking for a resolution to this problem.

I need to offer apologies to whomever posted the solution I ended up using; I’ve dabbled with IIS before but I did a lot of searching, and trying of potential solutions, before I got a working one but I accidentally closed the browser and have no idea where the solution originated.

In the root of my domain.co.uk website I created a folder called subdomain (not really but making the example generic) and then added the following to the web.config file of the website;


<rewrite>
   <rules>
      <rule name="Rewrite sub-domain to dir" enabled="true">
         <match url="^(.*)$" />
         <conditions>
            <add input="{HTTP_HOST}" pattern="^subdomain\.domain\.co\.uk$" />
         </conditions>
         <action type="Rewrite" url="subdomain/{R:1}" />
      </rule>
   </rules>
</rewrite>

When a user visits http://subdomain.domain.co.uk the returned page is actually loaded from http://www.domain.co.uk/subdomain without the user being aware of this and they can navigate around the sub-domain as if it was it’s own self-contained website.

The only downside is if someone knows that subdomain is a sub-folder of domain then they could navigate to http://www.domain.co.uk/subdomain, but this can be handled with a little PHP code to redirect these people to a 404 page.

Related posts:
How To Create A Domain Controller
Ian Grieve

About Ian Grieve

Consultant with over 8 years experience with Microsoft Dynamics GP, including related products such as Integration Manager, eConnect, FRx and Management Reporter.
This entry was posted in IIS 7, Internet Information Services (IIS), Microsoft and tagged , , , . Bookmark the permalink.

One Response to How To Rewrite A Sub Domain To A Directory In IIS

  1. Pingback: Interesting Findings & Knowledge Sharing » How To Rewrite A Sub Domain To A Directory In IIS | azurecurve

Leave a Reply

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

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>