Assigning a Virtual Domain Name to Your Localhost

This quick guide will walk you through the process of setting up a virtual domain name on Apache so that you can easily access your local "website" via a local network. This would be a good first step in setting up an intranet.

With that in mind, let's pretend we are setting up an intranet for a small business and want users to be able to access it via http://intranet/. And, for the sake of example, let's say that your computer's network IP address is Here's the process:

Add Your Virtual Host to httpd.conf

…Actually, you'll probably be editing httpd-vhosts.conf, but start by looking in your httpd.conf file to make sure. If you see something like this, you'll need to find and edit the file that it references:

1.   # Virtual hosts
2.   Include conf/extra/httpd-vhosts.conf

Within that httpd-vhosts.conf file, you should see something that looks like this:

1.   ##<VirtualHost *:80>
2.   	##ServerAdmin
3.   	##DocumentRoot "C:/xampp/htdocs/"
4.   	##ServerName
5.   	##ServerAlias
6.   	##ErrorLog "logs/"
7.   	##CustomLog "logs/" common
8.   ##</VirtualHost>

That's an example virtual host commented out with the ## before each line so it will be ignored. Leave it there for future reference, but copy and paste another version right below it and get rid of the pound signs to uncomment it. Next, you're going to want to edit the information:

1.   <VirtualHost *:80>
2.   	ServerAdmin
3.   	DocumentRoot "C:/xampp/htdocs/intranet"
4.   	ServerName intranet
5.   	ServerAlias localhost
6.   	ErrorLog "logs/intranet-error.log"
7.   	CustomLog "logs/intranet-access.log" common
8.   </VirtualHost>

Save that file, then restart Apache. At this point, Apache knows to direct any traffic coming in to that address to the appropriate folder. But you still need to map out the address on the computers in the network (including yours).

Edit Your Local Host File | Windows

This next part tells Windows that whenever you type http://intranet/ into your browser, you want to access Apache on (Note: For your local computer, use instead of your machine's network IP address.)

Because of security settings, you'll likely need to be running Notepad as an administrator. To do that, click the Start button, find Notepad, right mouse click on it and select "Run as Administrator". After Notepad opens up, open up C:\WINDOWS\system32\drivers\etc\hosts (it might be in a slightly different location depending on your version of Windows and your installation options.

You should see some comments up top and then this:

1.	   localhost
2.   ::1			 localhost

Those are your computer's IPv4 and IPv6 local addresses. Below them, you'll want to add your new entry:

1.	   localhost
2.   ::1			 localhost
3.   intranet

Save the file and then browse to http://intranet/ and you should see your new intranet. Pretty easy, right?

Edit Your Local Host File | Mac OSX

If you're on a Mac, follow these steps instead to edit your local host file. Similar to what we did in Windows, you'll need to edit as a superuser. To do so, open up Terminal and then type this:

1.   sudo nano /etc/hosts

That will bring up a text editor with a file that includes mappings between IP addresses and hostnames. You'll want to add your new entry to the end:

1.   intranet

Exit out of the editor (save when prompted), close down terminal and then open your browser and navigate to http://intranet/. You should be all set.

Leave a Comment

Name and email are required. Your email will not be published.

This post was published on March 21st, 2014 by Robert James Reese in the following categories: Apache, OSX, and Windows. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.