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.

Ian Grieve

About Ian Grieve

Ian is a Microsoft Dynamics GP certified consultant specialising in the delivery of Microsoft Dynamics GP projects and currently working for Perfect Image Ltd., a Microsoft Partner and VAR in the North East of England. Ian has worked with Microsoft Dynamics GP since 2003 and, over the nine years since, has dealt with all aspects of the product life-cycle from presales, to implementation, to technical and functional training, to post go-live support and subsequent upgrades and process reviews. In his spare time, Ian runs the azurecurve | Ramblings of a Dynamics GP Consultant blog dedicated to Microsoft Dynamics GP and related products.
This entry was posted in IIS 7, Internet Information Services (IIS), Microsoft and tagged , , , . Bookmark the permalink.

3 comments on “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

  2. m trying to rewrite my subdomain like keshav.websonet.in to websonet.in/keshav but when i write ur code to my website , then it show IIS7 error page.

    this error generally come when there is subdomain create via panel.

    m using website panel

    • I’ve only ever done this directly into the web.config file using Notepad++.

      What does the web.config look like once you’ve updated it via the control panel?

      Ian

Leave a Reply

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

*

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>