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


web notification

15 comments:

  1. nice script.

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

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

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

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

    ReplyDelete
  5. 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.

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

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

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

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

    I will do this

    ReplyDelete
  10. Hi,

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

    ReplyDelete
  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?!

    ReplyDelete
  12. Where you have hosted your demo tutorials and which domain registrar you used to create "demos.9lessons.info"

    ReplyDelete

mailxengine Youtueb channel
Make in India
X