The best thing to do if you are running *AMP is to do what Thomas suggests and do virtual hosts in Apache. You can do this either with or without the redirect you describe.
Virtual hosts
Most likely you will want to do name-based virtual hosts, as it's easiest to set up and only requires one IP address (so will also be easy to set up and test on your local MAMP machine). IP-based virtual hosts is better in some other respects, but you have to have an IP address for each domain.
This Wikipedia page discusses the differences and links to a good basic walk-thru of how to do name-based vhosts at the bottom.
On your local machine for testing, you'll also have to set up fake DNS names in /etc/hosts
for your fake test domain names. i.e. if you have Apache listening on localhost and set up vhost1.test.domain and vhost2.test.domain in your Apache configs, you'd just add these domains to the 127.0.0.1
line in /etc/hosts
, after localhost:
127.0.0.1 localhost vhost1.test.domain vhost2.test.domain
Once you've done the /etc/hosts
edit and added the name-based virtual host configs to your Apache configuration file(s), that's it, restart Apache and your test domains should work.
Redirect with mod_rewrite
If you want to do redirects with mod_rewrite (so that user.example.com
isn't directly hosted and instead redirects to example.com/user
), then you will also need to do a RewriteCond to match the subdomain and redirect it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomain$1 [R]
You can put this in a .htaccess
or in your main Apache config.
You will need to add a pair of rules like the last two for each subdomain you want to redirect. Or, you may be able to capture the subdomain in a RewriteCond to be able to use one wildcard rule to redirect *.example.com to example.com/
- -- but that smells really bad to me from a security standpoint.
All together, vhosts and redirect
It's better to be more explicit and set up a virtual host configuration section for each hostname you want to listen for, and put the rewrite rules for each of these hostnames inside its virtual host config. (It is always more secure and faster to put this kind of stuff inside your Apache config and not .htaccess
, if you can help it -- .htaccess
slows performance because Apache is constantly scouring the filesystem for .htaccess
files and reparsing them, and it's less secure because these can be screwed up by users.)
All together like that, the vhost config inside your Apache configs would be:
NameVirtualHost 127.0.0.1:80
# Your "default" configuration must go first
<VirtualHost 127.0.0.1:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /www/siteroot
# etc.
</VirtualHost>
# First subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
ServerName vhost1.example.com
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]
</VirtualHost>
# Second subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
ServerName vhost2.example.com
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]
</VirtualHost>