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

44

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.

Share:
44

Related videos on Youtube

user3519261
Author by

user3519261

Updated on September 18, 2022

Comments

  • user3519261
    user3519261 almost 2 years

    I'm trying to get this box to turn from "hitting endpoint" which it does when it's clicked to back to normal. But it does not seem to update its state.

    html:

    <div class="row">
            <div class="col-lg-6">
                <div class="form-group">
                    <button type="submit" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Hitting Endpoint.." class="btn btn-primary btn-lg" value="Hit Endpoint" id="submitButton" style="max-width: 1000px; height: 500px; margin-left: 250px; margin-right: 10px; margin-top: 20px; margin-bottom: 0px; width: 200px; height: 75px">Hit Endpoint</button>
                </div>
            </div>
    

    js:

    $("#submitButton").on("click", function () {
                    var request = $("#requestBox").val();
                    if (!request) {
                        alert("Please populate the request box with valid JSON");
                        return;
                    }
                    $("#submitButton").button('loading');
                    $.post('@Url.Action("Run")', { requestJson: request })
                            .success(function (data) {
                                var jsonPretty = JSON.parse(data);
    
    
      alert("Was successful");
                            renderjson.set_max_string_length("none");
                            renderjson.set_show_to_level(2);
                            document.getElementById("responseBox").appendChild(renderjson(jsonPretty));
                            $(window).scrollTop($('#pBox').offset().top);
                            $("#submitButton").button('reset');
                    })
    

    Using reset doesn't seem to be working. What am I doing wrong? Do I actually have to use setTimeout like the example here?

    http://codepen.io/jmalatia/pen/HkmaA

    Cheers

  • João Pimentel Ferreira
    João Pimentel Ferreira over 6 years
    can you tell the name and path of the apache2 configuration file?
  • João Pimentel Ferreira
    João Pimentel Ferreira over 6 years
    don't forget to restart apache after changes sudo service apache2 restart