11th
Host-Based Redirect in Apache
Here’s a recipe for redirecting by hostname in Apache. It’s like using Redirect but you can have more than one per VirtualHost container.
This is especially useful when you have a single SSL host with a lot of different sub-sites on it, and you want to provide the convenience of virutal host names to colleagues or clients. It’s obviously much easier for people to remember (and type) board.example.org than ssl.example.org/sites/board, and this technique makes it easy to provide that.
<VirtualHost *:80>
ServerAdmin webmaster@example.org
ServerName ops.example.org
ServerAlias board.example.org it.example.org
DocumentRoot /usr/share/apache2/htdocs
RewriteEngine On
RewriteCond %{HTTP_HOST} ops.example.org
RewriteRule ^(.*) https://ssl.example.org/sites/ops$1 [R=301]
RewriteCond %{HTTP_HOST} board.example.org
RewriteRule ^(.*) https://ssl.example.org/sites/board$1 [R=301]
RewriteCond %{HTTP_HOST} it.example.org
RewriteRule ^(.*) https://ssl.example.org/files/it$1 [R=301]
</VirtualHost>
These directives provide redirects for three different virtual hosts, subdomains of example.org. Each will redirect to a specific location at https://ssl.example.org/.
The $1 at the end of the RewriteRule causes the originally requested location to be appended to the redirect. In other words, a request for http://board.example.org/minutes.html will redirect to https://ssl.example.org/sites/board/minutes.html
Finally, the [R=301] flag causes Apache to issue a 301 Moved Permanently redirect, rather than the default 302. Some say this is better practice for search engines and such. That doesn’t really apply in this specific example (since these redirects are to secure sites) but it doesn’t hurt, either, and might save some browser overhead on subsequent requests for the virtual hostname.