Easy mod_rewrite Faux subdomains!
Okay, so recently a I ran across this age old question over at the good ole Pixel2Life forums the other day decided I was curious about it myself.
The best one I found came from WebDeveloper.com. I was able to get it up and working on the first try but the only thing I really didn’t like is that it redirected my browser to the PHP page, which I really didn’t like and looked quite ugly.
RewriteEngine On
# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld$ [NC]
# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www|ftp|mail)$ [NC]
# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^(.*)$ index.php?u=%1 [L]
So, this is what I came up with. Essentially, you can enter in “http://anything.domain.com” and “anything” will be passed to $_GET[”u”] without the user being visually aware of any PHP script being used.
htaccess mod rewrite php sub domains