~ <VirtualHost *:80> 
~ ServerAdmin webmaster@dummy-host.example.com 
~ DocumentRoot /usr/local/apache2/htdocs 
~ ServerName 
~ ErrorLog logs/dummy-host.example.com-error_log 
~ CustomLog logs/dummy-host.example.com-access_log common 
~ # rewrite environment This is for automatic https:// 
~ RewriteEngine on 
~ # redirect http to https 
~ # If you don't try to access https, then redirect to https 
~ RewriteCond %{SERVER_PORT} !^443$ 
~ RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 L,R? 
~ </VirtualHost?> 

Canonical Hostnames


The goal of this rule is 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. Solution:

For sites running on a port other than 80

RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) = http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R] 

And for a site running on port 80

RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]

Working Example:

<VirtualHost *:80>
       ServerName fw.dghartung.com
       ServerName barton.dghartung.com
       ServerName server.dghartung.com
       ServerName dghartung.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.dghartung.com$ [NC]
RewriteRule ^/(.*) http://www.dghartung.com/$1 [L,R]
Dale Hartung,
Mar 20, 2011, 3:14 PM