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