Setting virtual hosts for local computer

Posted on 2002-06-27
Medium Priority
Last Modified: 2013-11-18
I am a developer working on several projects.  Each of these is separate, and I'll likely have more in the future.  Each project has it's own domain and may be hosted in various places.  When I'm developing I like to have all the files on my local computer.  So I have several sub-directories, each one containing the files needed for that project.

If I want to view the files in a browser I do something like

That is fine, except now my designer is using some include files like this

So no matter where he wants that file included (meaning on a page that may be 2 sub-directories lower than the root of the web) it will grab includedfile.html from the root directory of the web.

This works fine for each projects live domain that is hosted on a production server.
So www.project1.com/somethings/directory/testfile.html
may have the include of /includedfile.html.
includedfile.html is located at www.project1.com/includedfile.html

That all works fine, but I now need to get that same functionality locally.
If I type in localhost/somethings/directory/testfile.html I don't get the included file because apache has /var/www/html as the document root for all on the server.

What I am trying to do is set up virtual hosting for each project on my local machine.  I would like to be able to use something like http://project1.localhost and get that directory of files.  So then I can type http://project1.localhost/somethings/directory/testfile.html and the includedfile.html will be there properly because project1.localhost has the document root set at /var/www/html/project1.

Then the different project files in each sub-directory won't interfere with each other.

Is there a way to accomplish this locally and what do I alter and set up?

Question by:mindarch
LVL 15

Accepted Solution

samri earned 800 total points
ID: 7113905

it is possible.

What platform is your local workstation.

One possibilities is to have the www.project1.com, www.project2.com, etc. to be defined in the local hosts file.  In Unix, check the file /etc/hosts/, or if you are running Win2K (or NT) chech the file in c:\winnt\system32\drivers\etc\hosts

For example Ihad this in my c:\winnt\system32\drivers\hosts file;     www.a.com     www.b.com     www.c.com     www.d.com

Next if to configure apache to do VirtualHosting.

Apache has a great docs site for virtual hosting;


--some simple example cut and paste from that site.

Running several name-based web sites on a single IP address.


Your server has a single IP address, and multiple aliases (CNAMES) point to this machine in DNS. You want to run a web server for www.example1.com and www.example2.org on
this machine.

      Note: Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in
      DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from
      the machine with those hosts entries.

Server configuration:

           # Ensure that Apache listens on port 80
           Listen 80

           # Listen for virtual host requests on all IP addresses
           NameVirtualHost *

           <VirtualHost *>
               DocumentRoot /www/example1
               ServerName www.example1.com

               # Other directives here

           <VirtualHost *>
               DocumentRoot /www/example2
               ServerName www.example2.org
               # Other directives here


Author Comment

ID: 7187966
Thanks.  Gave me what I needed.  

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question