Differences between revisions 13 and 14
Revision 13 as of 2012-10-31 16:52:46
Size: 1726
Editor: 2bb
Comment: Fix leading slash problem, which i encountered using the example on debian lenny / 2.2
Revision 14 as of 2014-09-16 13:18:59
Size: 1822
Editor: 212
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
NameVirtualHost *:80 # Required for 2.2.x and below:
NameVirtualHost *:80
Line 16: Line 17:
# www.example.net and www.example.com VirtualHost
Line 22: Line 24:
# Canonical VirtualHost
Line 23: Line 26:
  # Canonical host
Line 29: Line 31:

Canonical HostNames

Many sites map a variety of hostnames to the same content. For example, example.com, www.example.com and www.example.net may all refer to the same site. It is best to make sure that, regardless of the name clients use to access the site, they will be redirected to a single, canonical hostname, preferably without "www.". This makes the site easier to maintain and assures that there will be only one version of the site in proxy caches and search engines.

Using Virtual Hosts

Use the Redirect directive inside a VirtualHost block to force browsers to redo a request with a new host name.

Remember that the first listed VirtualHost will serve all requests that match no ServerName or ServerAlias. By using a Redirect in this host, you can assure that any non-matched names will also be transferred to the Canonical host.

# Required for 2.2.x and below:
#NameVirtualHost *:80

# www.example.net and www.example.com VirtualHost
<VirtualHost *:80>
  ServerName www.example.net
  ServerAlias www.example.com
  Redirect permanent / http://example.com/

# Canonical VirtualHost
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /usr/local/apache/htdocs

Using mod_rewrite

# For sites running on a port other than 80
RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !=""
RewriteCond %{SERVER_PORT} !=80
RewriteRule ^/?(.*)         http://example.com:%{SERVER_PORT}/$1 [L,R=301]

# And for a site running on port 80
RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !=""
RewriteRule ^/?(.*)         http://example.com/$1 [L,R=301]

CanonicalHostNames (last edited 2014-09-16 13:18:59 by 212)