Running Multiple Websites or Domains with Different Domain Names or Host Names with Apache Web Server

If you have read the post on adding hosts names for development on IIS or Apache you may have already started wondering how can you serve or show different content using IIS or Apache.
apache_header 
Here we will show you just how easy it is to configure IIS or Apache web server to serve different sites when different hostnames are used.
You may be running any application server, like ColdFusion, PHP, Ruby on Rails, ASP.net, Tomcat, you will find the following information handy.

This article will help you configure your Apache web server to run and handle multiple domain names.

How to configure Apache Web Server to run multiple websites or domains or sub-domains:
  1. Open httpd.conf file. This file is located inside the installation directory under /conf folder. Usually it is placed under: C:\Program Files\Apache Software Foundation\Apache2.2\conf
  2. Scroll to the bottom of the text file and add the following text:
    NameVirtualHost *:80
  3. For each new website we wish to add we add a vhost. You can copy and add the following lines:

    <VirtualHost *:80>
        ServerName sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/test/"
    </VirtualHost>

  4. Restart Apache and load sub.domain.com using the browser, if you’re lucky you will be able to access the pages inside the DocumentRoot specified in the previous step. You can also get an Access Denied or Directory listing denied error which will be fixed in the next step.
  5. Add this (only when you’re getting errors mentioned in Step 4) to make sure users can access the pages:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/"
    <Directory "C:/Apache2.2/htdocs/test">
        Options FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
    </Directory>
    </VirtualHost>
  6. You can do much more than this. You can specify other names for this new domain:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        ServerAlias www.sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/test/"
    </VirtualHost>
  7. No just that, you can add more than one name separating each with a single space:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        ServerAlias www.sub.domain.com sub2.domain.com sub.another-domain.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/"
    </VirtualHost>
  8. Now the final stroke adding one more site pointing to a separate domain:
    <VirtualHost *:80>
        ServerName sub.domain.com
        ServerAlias www.sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/sub/"
    </VirtualHost>
    <VirtualHost *:80>
        ServerName localhost
        ServerAlias www.localhost.com localhost.com local local.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/"
    </VirtualHost>
    <VirtualHost *:80>
        ServerName dev.domain.com
        ServerAlias http://www.dev.domain.com/
        DocumentRoot "D:/wwwroot/Inetpub/dev/"
    </VirtualHost>

 

Explanations:

NameVirtualHost *:80 Tells Apache Web Server that we want to listen to all requests made to any IP address bind to Apache. A simple IP-based virtual host will use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
<VirtualHost *:80>
A new web site or virtual host defined.
ServerName sub.domain.comThe hostname for this virtual host.
ServerAlias www.sub.domain.comAnother hostname for this virtual host. More can be added by separating hostnames with a spaces.

 

Other cool examples:

Listen 8080

<VirtualHost 111.22.33.44:8080>
DocumentRoot /www/domain2
</VirtualHost>
  1. Makes Apache listen to port 8080 as well.
  2. Create a virtual host with specific IP address and specific port as well.
<VirtualHost 111.22.33.44:*>
DocumentRoot /www/domain2
</VirtualHost>
Listens to all ports on the specified IP address.
<VirtualHost *:8080>
DocumentRoot /www/domain2
</VirtualHost>
Listens to specific port number on all IP addresses of the server.
<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>
Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
<VirtualHost _default_:80>
DocumentRoot /www/default80 
</VirtualHost>
<VirtualHost _default_:*>
DocumentRoot /www/default 
</VirtualHost>
  1. Same as the previous one,
  2. But the server listens on several ports and
  3. We want to use a second _default_ vhost for port 80
Note: Some old browsers lacks the ability to send Hostname header in the requests and so websites that depend solely on ServerName or ServerAlias won’t work. A work around is to point such browsers to a directory using rewrite rules. Doing this will make your website available to all kinds of browsers:
<VirtualHost 111.22.33.44>
# primary vhost
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html 
</VirtualHost>

<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1 
</VirtualHost>

<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub2
ServerName
www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1 
</VirtualHost>

Good Luck.

Comments

  1. sorry don't know what happened to list last line to complete my post above:



    tia

    ReplyDelete
  2. Ok, have had a fair go at redirecting to sub domain (?).

    Where I want my inTRAnet browser address of m.com to open my mobile.html on IIS 5.1 etc.

    But for now just trying to get a simple test q.com to redirect to C:\wamp\www\q

    q.com is just a address
    C:\wamp\www\q is just a test folder


    Before updating the httpd.conf my localhost was showing the folder list in C:\wamp\www and localhost:8888 was showing the my IIS 5.1 default web page as expected.

    But as soon as I add the following my localhost redirects to C:\wamp\www\q.

    And q.com loads some intranet web page.

    Any help would be greatly appreciated.

    ....

    Include "c:/wamp/alias/*" # last line of original httpd.conf

    #
    # remove simple dns server and make sure preferred dns 1 and 2 on tcp/ip are empty

    # if error: 403 - Forbidden You don't have permission to access / on this server
    # make sure apache is online and green icon in system tray




    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so


    #keep proxy to OFF or your server will act as a forward proxy
    #ProxyRequests Off directive requires LoadModule mo_proxy.so and mod_proxy_http.so

    ProxyRequests Off


    NameVirtualHost *:80

    ServerName q.com
    ServerAlias www.q.com www.q

    #DocumentRoot CANNOT end with a \" nor #
    DocumentRoot "C:\wamp\www\q"




    Thanks heaps,
    Barry G. Sumpter

    ReplyDelete
  3. On forget it.
    Too much time trying to get it to run.
    Removing Apache and returning to iis 5.1

    ReplyDelete
  4. i hav apache on port 80 and iis on other port 8080.
    i have Included this in httpd.conf
    NameVirtualHost *:80


    ServerName www.my-site.com
    ServerAlias *.my-site.com
    DefaultType text/html

    Options Indexes FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all

    # sends all requests made to port 80 to another port 8080
    ProxyPass / http://www.my-site.com:8080/
    #makes proper changes to server-side redirection header
    ProxyPassReverse / http://www.my-site.com:8080/


    problem is:
    when i open http://localhost
    then it opens the iis site on port8080 through apache.
    but how to open ? apache webpage on http://localhost:80
    in apache i have Listen *:80

    ReplyDelete

Post a Comment

Be relevant. Spammers will be banned.

Popular posts from this blog

Installing ColdFusion on IIS 7 + Configure IIS for ColdFusion in Windows Vista and Windows Server 2008

ColdFusion Manual Configuration & Issues with TCP ports 51011, 51010 and 51800 on IIS and Apache

Configuring Apache Tiles to Avoid Caching During Development