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.

What should we write about next?

If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.

Your Name

Your Email

Suggested Topic

Suggestion Details

Looking for support or consultancy with Microsoft Dynamics GP?

I no longer work with Microsoft Dynamics GP, but the last company I worked for was ISC Software in the UK; if you’re looking for support or consultancy services with Microsoft Dynamics GP you can contact them here.

5 thoughts on “How To Rewrite A Sub Domain To A Directory In IIS

  1. keshav says:

    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

    1. Ian Grieve says:

      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

  2. Harish Balakrishnan says:

    This works and I was able to see it in action for one subdomain but if I got 2 or more, should I add a new rule each time to this configuration. Adding another block, got both not working. Any suggestion.

    1. Ian Grieve says:

      I do add new ones by adding a whole new rule block.

Leave a Reply

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