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
Warning: Declaration of Social_Walker_Comment::start_lvl(&$output, $depth, $args) should be compatible with Walker_Comment::start_lvl(&$output, $depth = 0, $args = Array) in /Users/wade/Sites/hm2k.org/wp-content/plugins/social/lib/social/walker/comment.php on line 18
Warning: Declaration of Social_Walker_Comment::end_lvl(&$output, $depth, $args) should be compatible with Walker_Comment::end_lvl(&$output, $depth = 0, $args = Array) in /Users/wade/Sites/hm2k.org/wp-content/plugins/social/lib/social/walker/comment.php on line 42
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?
i cant get it to work
i want this http://subdomain1.portal/ from http://portal/subdomain1/
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
don’t work
why?
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/
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
Without adding wildcard subdomains to the DNS I’m not sure how this would work.
This works great! It fixed my problem perfect! Thank you! 🙂
[…] nicht mehr sicher, aber lassen sich nicht mithilfe von mod_rewrite virtuelle subdomains anlegen? creating-subdomains-from-directories-using-mod_rewrite-in-apache-htaccess […]
Dear sir, I need to Create the Sub Domains Using .htaccess with out use of my Domain Control Panel it is Possible…. Pls let me know how?
how to access direct throught sub domain of main domain using .htaccess in php?