How to point 2 different domains to 1 IP address (Apache)?

I have two different domain names, which I want to host on the same private hosting I have. I want them to point to the same IP address and yet have different content (of course). How can I do that? Are there different ways to do this? In that case what is the best way. Thanks.


Solution 1:

You'll probably want to use Apache's Virtual Hosting. It's a well-supported method for hosting many domains on a single IP address.

http://httpd.apache.org/docs/2.2/vhosts/

Specifically, name-based virtual hosts: http://httpd.apache.org/docs/2.2/vhosts/name-based.html

Solution 2:

Apache2 supports serving different contents depending on domain names, even if all those domains resolve to the same IP address. Each of the domain names is handled by a virtual host, thus the name name-based virtual host.

Here is a sample configuration for two domains:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName www.yourfirstdomain.tld
  DocumentRoot /www/yourfirstdomain.tld/
</VirtualHost>

<VirtualHost *:80>
  ServerName www.yourseconddomain.tld
  DocumentRoot /www/yourseconddomain.tld/
</VirtualHost>

You will find the official documentation on name-based virtual host support here.

However, please note this will only work with plain HTTP connections, not with HTTPS (HTTP over SSL): name-based virtual hosts rely on knowing what name is being requested, but this information can't be known by Apache until the encrypted SSL connection is established.

If you need to have your setup working with HTTPS too, you need to rely on an extension to the SSL protocol called Server Name Indication (SNI)(RFC4366). Basically, SNI-enabled clients (ex: web browsers) add an extra plain text information when establishing the encrypted SSL connection, allowing the server to know the name being requested before the SSL connection is ready.

All browsers do not support SNI yet. At the time of writing and according to Wikipedia, these ones do:

  • Internet Explorer 7 or later, on Windows Vista or higher (does not work on XP, even IE 8)
  • Mozilla Firefox 2.0 or later
  • Opera 8.0 or later (the TLS 1.1 protocol must be enabled)
  • Opera Mobile at least version 10.1 beta on Android
  • Google Chrome (Vista or higher, XP on Chrome 6 or newer, OS X 10.5.7 or higher on Chrome 5.0.342.1 or newer)
  • Safari 2.1 or later (Mac OS X 10.5.6 or higher and Windows Vista or higher)
  • Konqueror/KDE 4.7 or later
  • MobileSafari in Apple iOS 4.0 or later
  • Android default browser on Honeycomb (v3.x) or newer
  • Windows Phone 7
  • MicroB on Maemo

Hosting several name-based virtual hosts accessible trough HTTPS requires you to enable mod_ssl and add a similar configuration as in the example above:

Listen 443      
NameVirtualHost *:443

# Accept connections for these vhosts from non-SNI clients
# Clients without SNI will be handled by the first defined vhost.
# If you only want SNI-enabled client, put on instead
SSLStrictSNIVHostCheck off

<VirtualHost *:443>
  ServerName www.yourfirstdomain.tld
  DocumentRoot /www/yourfirstdomain.tld/    
</VirtualHost>

<VirtualHost *:443>
  ServerName www.yourseconddomain.tld
  DocumentRoot /www/yourseconddomain.tld/
</VirtualHost>

Remember that using SSL requires you to buy SSL certificates for you domains - or to generate some auto-signed ones, and to configure Apache to use them for SSL connections.