Multiple Virtual Domains with One IP Address

Discover two ways to set up virtual domains with one IP address.

Tong Lai Yu

June 30, 1997

7 Min Read
ITPro Today logo

Setting up virtual domains in Windows NT is straightforward. In September1996 ("How to Set Up Virtual Domains"), I described how to assign aunique IP address to each domain. However, in practice, you might want to set updomains that share an IP address, for several reasons. For example, because of abug in the NT 3.x design, you can add only about 14 different IP addresses inthat system unless you have installed Service Pack 5. In NT 4.0 Workstation, youcan add about 10 IP addresses in the system; if you add more, you risk runninginto legal battles with Microsoft. Moreover, many Web servers-- inparticular, those running on Windows 95 or on the Macintosh--do not supportmultiple IP addresses. In these circumstances, sharing an IP address fordifferent domains is the best solution for setting up virtual domains. Let'sexplore how sharing an IP address works and look at two ways of setting upvirtual domains with one IP address.

Our goal is to set up different domains, such as abc.com and xyz.com, sothat when users browse http://www.abc.com or http://www.xyz.com, the systemdisplays the respective home pages of companies ABC and XYZ, even though thecompanies have the same IP address (207.68.156.100). You can accomplish thisfeat with a simple script. The method works for more than two domains, too. Iused Computer Software Manufaktur's Alibaba 2.0 Web server running NTWorkstation to illustrate the techniques, but you can use the same method withMicrosoft Internet Information Server (IIS) or any other contemporary Webserver.

The Domain Name System Name Server
First, set up the Domain Name System (DNS) name server in your Networkproperly. To make two domains share an IP address, you can set up a canonicalname (CNAME) for aliases. (A CNAME is the official name of the system host andis specified in the address (A) record for the host.) Create a CNAME record foreach alias that shares the address. When a name server looks up a host name ordomain name and finds a CNAME record, the server replaces the host or domainname with the CNAME and looks up the CNAME. If you use the Berkeley InternetName Domain (BIND) software in your name server, you must add the following twoentries to the primary named file:

www.abc.com. IN A 207.68.156.100
www.xyz.com. IN CNAME www.abc.com.

(BIND, usually included in UNIX-based operating systems, is the basis forthe Windows NT 4.0 DNS. See Spyros Sakellariadis, "Configuring andAdministering DNS," August 1996.) In the NT machine, add the network IPaddress 207.68.156.100. Now whether you ping www.abc.com or www.xyz.com, youobtain the same response: Reply from 207.68.156.100.

Set Up the Document Root
In your NT Web server, set up the document root directory, e:, for the IPaddress 207.68.156.100. Create two subdirectories, e:abc and e:xyz, for thedomains abc.com and xyz.com, respectively. The home pages of the two companiesreside in those two subdirectories and subsequent subdirectories. For example,company ABC can set up a subdirectory e:abcimages to hold all the images thatit needs. At this point, create the default file index.htm (or the appropriatedefault entry file of your Web server) for a home page and put it in thedocument root directory. Now when users browse either http:\www.abc.com orhttp:\www.xyz.com, they will see the same home page.

To display different home pages for the two universal resource locators(URLs), you must redirect the requests to the appropriate directories andexecute the correct files. A simple way to do this task is to add theappropriate index.htm file, or subdirectory, to the URL or to the link to thecompany on a related home page; when users request that URL or click on thatlink, they will go to the right subdirectory. The following sections explore twomethods to make this process transparent to the user.

The CGI Method
First, let's first explore using Common Gateway Interface (CGI) scripts toredirect requests. You can automatically start a new URL by making use of theclient-pull technique. In this technique, the browser either reloads thecurrent page or loads a different page after a specified delay. In this way, Webdocument contents can change without action from the user. One way to redirect aURL is to edit the default start file index.htm (in e:) so that it contains thestatements:

"Refresh" content="0;url=/cgi-bin/referer.exe">

where content="0 informs the server to start (i.e., refresh)the URL specified in the line after 0 seconds (i.e., immediately). The filereferer.exe is an executable CGI script that has been compiled from the C-sourcecode (referer.c) in Listing 1 to direct the user to the correct domain.

In Listing 1, the statement

getenv("HTTP_REFERER");

returns the link that leads to the execution of the CGI script. The function

strstr()

searches for the existence of a substring that returns NULL if the substringdoes not exist. Through these steps, the command Refresh will start the correcthome page. For example

printf( "Refresh: 0; URL=http://www.xyz.com/xyz/index.htm" );

starts the URL link http://www.xyz.com/index.htm after 0 seconds--thatis, immediately.

This method is a simple way to redirect the user. You can easily modify thecode to make the URL-lookup efficient when you have a large number of URLs. Forexample, you can put all the URLs in a table and use standard searchingtechniques, such as hashing or binary searching to perform a quick table lookup.Unfortunately, most browsers currently do not return the appropriateURL-referrer when you start the CGI by Refresh instead of by hand-clicking theURL link. Although I believe that Refresh will work in the future, to use theCGI method now, you must click at the prompt, and the file index.htm in e: willbe

Users browsing http://www.abc.com or http://www.xyz.com will see the prompt

Please click here

You must click on this message to start the correct page. Alternatively, youcan use the tag

in an HTML page to perform the page refresh.

If you want to automate the whole process to work under browsers in usetoday, you must use JavaScripts.

The JavaScript Method
JavaScript lets you embed JavaScript commands in an HTML page to call up thecorrect home page. When you use a compatible Web browser, such as InternetExplorer (IE) 3.0 or Netscape Navigator 2.0 or higher, the browser loads yourJavaScript commands as part of the HTML document. You trigger the commands byclicking the buttons that Java displays on a related home page or the browserevaluates the commands as it downloads the script. Thus, the scripts can makeWeb pages execute dynamically. Listing 2presents the complete JavaScript codethat makes the URL-redirection transparent to the user. Place this code in thefile index.htm (or the default entry file of your Web server) in the documentroot directory of the Web server.

When the Web browser downloads the HTML file index.htm, the browserevaluates the JavaScript code go(), which calls the function go(). The functiongo() calls up the correct URL link. In the function, the variable location.href contains the current URL (i.e., either http://www.abc.com/index.htm orhttp://www.xyz.com/index.htm) and is compared with variable x. I set x tobe the string http://www.r, which has a string value between http://www.abc.comand http://www.xyz.com. Thus, if location.href is larger than x,the reference URL is http://www.xyz.com; otherwise, it is http://www.abc.com.

After determining the correct reference URL, you can redirect the browserto the correct home page by assigning the appropriate URL link to location.href.For example, the statement

location.href="http://www.abc.com/index1.htm"

automatically informs the browser to download the file index1.htm, whichresides in the document root directory. This file (index1.htm) is the entrypoint for the home pages of the company abc.com. (Alternatively, you can put thecompany-specific index.htm file in subdirectory e:abc with the statementlocation.href="http://www.abc.com/abc/index.htm".)

This script can make the single-address virtual domain setupuser-transparent; that is, when you use a compatible browser to browse the URLhttp://www.abc.com based on this setup, the effect will be the same as browsinga URL whose setup is based on multiple IP addresses.

I admit that this method works only if the browser supports JavaScript. Forobsolete browsers, you must rely on the manual CGI method. But either methodlets you stretch a limited number of IP addresses.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like