Creating subdomains from directories using mod_rewrite in Apache .htaccess

The idea was to have the ability to create unlimited subdomains simply by creating an appropreate directory for it in your html root directory.

Since most people don’t have direct access to their httpd.conf, the obvious solution was to create a method using mod_rewrite within “.htaccess”. This also allowed it to be setup very easily and quickly.

I have no immediate use for this solution, however I know it will come in very handy in the future.

Someone I know was trying to figure this out earlier today, so I took it upon myself to figure out how to work out a solution for this problem.

After much discussion with #apache @ EFnet, in particular TBF, we came about the following solution.

#Grab the subdomain from the domain
RewriteCond %{HTTP_HOST} ^([^.]+).hm2k.org$
#Make sure the subdomain is not www or example
RewriteCond %{1} !^(www|example)$
#Check if the directory actually exists before we go there
RewriteCond /home/hm2k/public_html/%1 -d
#This stops it from looping
RewriteCond %{REQUEST_FILENAME} !^/home/hm2k/public_html/
#Finally, this is the actual rewrite
RewriteRule (.*) /home/hm2k/public_html/%1/$1 [Last]

Thanks to all those who helped, I hope this comes in useful to someone.

1136331104

Related posts:

  1. Friendly URLs (revisited) Turn dynamic URLs into friendly URLs I’m sure we’re all...
  2. Focus on one domain Why you should be focusing on one domain name for...
  3. Storing mySQL database settings for php and perl in one file I have a situation where there’s two scripts. The main...
  4. suPHP and .phps PHP code highlighting support Today a user on one of my web servers asked...

8 Comments »

  1. Justice McCay said,

    August 20, 2007 @ 12:29 am

    How can I go about redirecting a directory to a subdomain using modrewrite?

    I have already set-up the subdomain in my control panel. How can I go about making site.com/oldname to newname.site.com using modrewrite?

  2. md5hash said,

    August 23, 2007 @ 8:35 am

    i cant get it to work

    i want this http://subdomain1.portal/ from http://portal/subdomain1/

  3. Rickywh said,

    November 15, 2007 @ 11:06 pm

    I tried this just now, replaced hm2k.org in the first line with my domain, then ran phpinfo() to get my DOCUMENT_ROOT, which is set to /home/rickywh/domains/mydomain.com/public_html

    i then changed set this path in the three lines that are relevant and saved it to a file called .htaccess, when i visited my main domain by going mydomain.com, i got internal server error message. but when trying to go to subdomain.mydomain.com, it displayed the message about it being a shared website that hast multiple users or something, so i guess it half works. hehe

  4. ivailo sym said,

    March 24, 2008 @ 5:04 pm

    don’t work
    why?

  5. OMG said,

    July 11, 2008 @ 12:50 am

    Omg it doesn’t work … searching everywhere and found a big nothing about how to make SubDomains with the help of the .htaccess if you don’t have access to httpd.conf. When I tipe http://subdomain.mydomain.com I need it to go to htdocs/subdomain/

  6. Al V said,

    September 12, 2008 @ 5:11 pm

    This solution is exactly what I need and have been unable to find. I added my site information without success.

    Your solution was pasted into my Document Root “.htaccess” file under

    RewriteEngine on

    All instances of “hm2k.org” were replaced with “mydomainname”

    “www|example” was replaced with “mydomainname”

    “REQUEST_FILENAME” was replaced with “REQUEST_subdomainname”

    After checking and rechecking, I am unable to determine what I am doing wrong. Any help will be appreciated.

    Thanks,

    Al

  7. Snippissimo said,

    November 1, 2008 @ 12:02 am

    Without adding wildcard subdomains to the DNS I’m not sure how this would work.

  8. geekgirl4christ said,

    June 16, 2009 @ 7:54 pm

    This works great! It fixed my problem perfect! Thank you! :)

RSS feed for comments on this post · TrackBack URL

Leave a Comment