Dynamic virtual hosts
You want to have each user on your system automatically have username.domain.com mapped to their home directory.
The first RewriteCond skips requests for content from www.domain.com, which we want to be handled via the regular mechanism. It may also be useful to exclude other hostnames which you don't want to get mapped to home directories.
The second RewriteCond exists to capture which hostname was requsted. When matches are captured (by using parentheses) in a RewriteCond, the captured pattern goes into the variables %1, %2, %3, and so on. In this case, a request for a hostname of username.domain.com will result in %1 being set to username.
Finally, our RewriteRule captures the entire requested URL, and causes it to be served out of that user's home directory. For example, a request for http://rbowen.domain.com/foo/bar.html will result in the file /home/rbowen/www/foo/bar.html being served.
It is important to note that you will still need to make the necessary changes to your DNS server to make these hostnames work. Generally, this would be done by creating a wildcard DNS entry mapping *.domain.com to this server. You will need to consult the documentation for your particular DNS server as to how to accomplish this.
For a more complex mapping, see RewriteMap
You have a virtual host with several values for ServerAlias. However, you'd like to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.
There are a variety of reasons that you may wish to force the use of one particular host name in preference to another. The most commons ones are
- search engine optimation (example.com and www.example.com are two different sites)
- cookies which will work only on a particular hostname
- SSL, which necessitates that the hostname be used which matches the SSL certificate
- Authentication, which needs to be on the right hostname for the browser to recognize it