9lessons programming blog
Loading Search
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 labs.9lessons.info and demos.9lessons.info. 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


Sponsored Links

Share this post

Comments
{ 10 comments }
Mitendra said...

nice script.

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

Anonymous said...

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

Anonymous said...

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.

Dat Tai said...

hello

cool site

Bergi said...

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

Daniel Kaiser said...

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.

Anonymous said...

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

fare said...

fake sub domain thanx... nice code.

only said...

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

si dekil said...

Hai..Thanks for this simple and powerful idea.

I will do this

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension