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

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
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














nice script.
do you have idea about how to replace space with - like:-(mitendra chaurasia should be mitendra-chaurasia)
I do not work. my hosting package, limited to 2 sub-domain name. I tried this method. failed. may exceed this limit, otherwise I?
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.
hello
cool site
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 :)
And if i have a domain.com and want to migrate it to weblog.domain.com, but maintain a new site on domain.com
the .htacess would not work this way, since it wou8ld redirect all files, and i want the domain.com to remain intact.
that was helpful, working on an old fashioned site that doesn't automate this and had to do this the hard way. :)
fake sub domain thanx... nice code.
what i write in my php link after completed rewrite rule.
Can any one help me?
Hai..Thanks for this simple and powerful idea.
I will do this