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.
ReplyDeletedo you have idea about how to replace space with - like:-(mitendra chaurasia should be mitendra-chaurasia)
Use php to replace whitespace with -
DeleteI do not work. my hosting package, limited to 2 sub-domain name. I tried this method. failed. may exceed this limit, otherwise I?
ReplyDeleteMitendra, 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.
ReplyDeleteAnonymous, 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
ReplyDeletecool site
I found this very useful:
ReplyDeleteVirtualDocumentRoot /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
ReplyDeletethe .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. :)
ReplyDeletefake sub domain thanx... nice code.
ReplyDeletewhat i write in my php link after completed rewrite rule.
ReplyDeleteCan any one help me?
Hai..Thanks for this simple and powerful idea.
ReplyDeleteI will do this
Hi,
ReplyDeletewww.mywebsite.com/my_products.php?sub_id=100000027
Should be
www.mywebsite.com/my_products/subcategory
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.
Hi,
ReplyDeleteI'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?!
Hi,
ReplyDeletenice script.
Where you have hosted your demo tutorials and which domain registrar you used to create "demos.9lessons.info"
ReplyDelete