Differences between version 3 and revision by previous author of NameVirtualHosting.
Other diffs: Previous Major Revision, Previous Revision, or view the Annotated Edit History
Newer page: | version 3 | Last edited on Saturday, February 24, 2007 3:29:04 pm | by AristotlePagaltzis | Revert |
Older page: | version 2 | Last edited on Wednesday, July 27, 2005 5:25:15 pm | by JohnMcPherson | Revert |
@@ -1,31 +1,62 @@
-The process of virtually hosting multiple websites on a single IP address by using the requested name in the [HTTP] header.
-
-----
-
An example for [Apache] would be:
-
-
in httpd.conf:
+The process of virtually hosting multiple websites on a single [
IP]
address by using the requested name in the [HTTP] header. An example for [Apache] would be the following configuration
in <tt>
httpd.conf</tt>
:
<verbatim>
-NameVirtualHost 12.34.56.78
+NameVirtualHost *
-<VirtualHost 12.34.56.78
>
+<VirtualHost *
>
ServerName example.com
ServerAlias www.example.com
- ServerAdmin yourmum
@example.com
+ ServerAdmin webmaster
@example.com
DocumentRoot /var/www/virtual/example.com
CustomLog /var/log/apache/example.com-access.log combined
ErrorLog /var/log/apache/example.com-error.log
</VirtualHost>
+<VirtualHost *>
+ ServerName example.net
+ ServerAlias www.example.net
+ ServerAdmin webmaster2@example.net
+ DocumentRoot /var/www/virtual/example.net
+ CustomLog /var/log/apache/example.net-access.log combined
+ ErrorLog /var/log/apache/example.net-error.log
+</VirtualHost>
+</verbatim>
+
+If the server has more than one [IP] address and you want it to serve a particular site only one a specific one of these addresses, you can replace the asterisks in that configuration with a particular address and optionally a port, eg.
+
+<verbatim>
+NameVirtualHost 12.34.56.78:80
+
+<VirtualHost 12.34.56.78:80>
+ ServerName example.com
+ ...
+</verbatim>
+
+The arguments of the <tt>~NameVirtualHost</tt> and corresponding <tt>~VirtualHost</tt> directives must match ''exactly''. Of course, if your server has several [IP]s you probably want several <tt>~NameVirtualHost</tt> directives too – one for every [IP]. You can then serve a different set of sites from each [IP].
+
+Note that you can also use things like “<tt>*:80</tt>” to serve a particular site only on a particular port, but on every [IP] of the server.
+
+If you have a site that can be reached through several domains/hostnames, eg. with or without the <tt>www.</tt> and either <tt>.com</tt> or <tt>.co.nz</tt>, you might want to consider making one of them the canonical address. This is better for any number of reasons: it will make your referrer logs easier to process; it will make things more obvious for search engines (might improve your ranking!); people will use almost exclusively the canonical address to link to your site, making it easier to find out who is linking to you; and on and on. With Apache virtual hosting, you can do this easily; in the following example, <tt>www.example.co.nz</tt> was chosen as the canonical address:
+
+<verbatim>
+NameVirtualHost *
-<VirtualHost 12.34.56.78
>
- ServerName example2.com
- ServerAlias
www.example2
.com
- ServerAdmin yourmum
@example2
.com
- DocumentRoot /var/www/virtual/example2
.com
- CustomLog /var/log/apache/example2
.com
-access.log combined
- ErrorLog /var/log/apache/example2
.com
-error.log
+<VirtualHost *
>
+ ServerName www.example
.co.nz
+ ServerAdmin webmaster
@example
.co.nz
+ DocumentRoot /var/www/virtual/example
.co.nz
+ CustomLog /var/log/apache/example
.co.nz
-access.log combined
+ ErrorLog /var/log/apache/example
.co.nz
-error.log
+</VirtualHost>
+
+<VirtualHost *>
+ ServerName example.co.nz
+ ServerAlias example.com
+ ServerAlias www.example.com
+ RedirectMatch 301 /?(.*) http://www.example.co.nz/$1
</VirtualHost>
</verbatim>
-The Name or IP used in
the ~NameVirtualHost directive must be
the same name or ip used
in each
~VirtualHost
.
+With this configuration, any and every request for a page on one of
the alias domains, eg. <tt>
~http://example.com/products/</tt>, will redirect the visitor
the same page in the canonical domain,
in this case <tt>
~http://www.example.co.nz/products/</tt>.
+
+There are other ways to achieve this, such as using mod_rewrite, but none of them is as simple as this one
.