Redirect The Sub Domain To a Sub Folder with .htaccess
Wall Script
Wall Script
Monday, January 11, 2010

Redirect The Sub Domain To a Sub Folder with .htaccess

In this post I want to explain " How to redirect the sub domain to a sub folder with .htaccess". I had implemented this for and I hope it is useful for you.

Redirect The Sub Domain To a Sub Folder with .htaccess

Example .htaccess code
You have to replace your sub domain and sub folder name.
RewriteEngine On

RewriteCond %{HTTP_HOST} ^demos\.9lessons\.info$
RewriteCond %{REQUEST_URI} !^/demos/
RewriteRule (.*) /demos/$1

RewriteEngine On

RewriteCond %{HTTP_HOST} ^labs\.9lessons\.info$
RewriteCond %{REQUEST_URI} !^/labs/
RewriteRule (.*) /labs/$1

web notification


  1. nice script.

    do you have idea about how to replace space with - like:-(mitendra chaurasia should be mitendra-chaurasia)

  2. I do not work. my hosting package, limited to 2 sub-domain name. I tried this method. failed. may exceed this limit, otherwise I?

  3. Mitendra, spaces in URIs are usually encoded (which is why you often see those percentages followed by numbers). I'm not sure whether mod_rewrite is aware of that, but if you can't catch spaces as literals, you might want to check for "%20" (without the quotes), which is the URI encoding for regular spaces.

    Anonymous, it doesn't work with your hosting package because the hosting package doesn't forward unknown subdomains to your web root.

    The way this trick works is that the web server redirects a request for http://mysubdomain.mydomain.tld to the site configured at http://mydomain.tld (or whatever the default would be) and the web root of that web site contains a .htaccess file which redirects requests for URIs starting with http://mysubdomain.mydomain.tld to a sub-folder.

    Another important consideration is that the URIs is only rewritten internally, the user is not forwarded at any point, that is, his browser doesn't receive a redirect. Only this way can you mask the real path http://mydomain.tld/mysubdomainfolder/… as http://mysubdomain.mydomain.tld/… — otherwise it would change what the user sees in the browser's location bar.

    Also, if you are using this to put part of your website on a different subdomain and you have some way of authenticating visitors (e.g. a log-in system) or other things which rely on cookies, remember that cookies are only valid for exact domain names, including subdomains. So a cookie for mysubdomain.mydomain.tld can't be read by www.mydomain.tld or just mydomain.tld — the browser won't let you access it.

  4. I found this very useful:

    VirtualDocumentRoot /www/%-2-%-1/%-3/

    So DocumentRoots would be following:
    - domain.tld -> /www/domain-tld/_/
    - test.domain.tld -> /www/domain-tld/test/

    Hope it helps someone :)

  5. And if i have a and want to migrate it to, but maintain a new site on

    the .htacess would not work this way, since it wou8ld redirect all files, and i want the to remain intact.

  6. that was helpful, working on an old fashioned site that doesn't automate this and had to do this the hard way. :)

  7. fake sub domain thanx... nice code.

  8. what i write in my php link after completed rewrite rule.
    Can any one help me?

  9. Hai..Thanks for this simple and powerful idea.

    I will do this

  10. Hi,

    Should be

    let me have .htaccess code for the above. Similar to this we have lot more pages.

    Let me have your idea on this.
    If would be more helpful, if I receive it asap.

  11. Hi,
    I'm struggling with your code in fact… I know that on my dedicated server, I have to allow subdomains but I thought to try it first on my localhost to get use to it, BUT, something went wrong… Do I have to change something to make it work on local or it's just bad luck?!

  12. Where you have hosted your demo tutorials and which domain registrar you used to create ""


mailxengine Youtueb channel
Make in India