Apache localhost and vhosts

I have Apache 2.2 installed on Windows, the folder that serves all my websites is:

C:\Users\GSTAR\Documents\Web Sites


Within this folder there are subfolders for each website

I currently access these websites on my server by navigating to http://localhost/site1, http://localhost/site2, etc.

The problem I have now is that some of the websites have relative URL paths, and this causes problems as resources are not found (it tries to look in the root folder).

I don't particularly wish to use absolute URLs. I had a look into vhosts, as this might be a solution. But I am having some trouble with this.

I wish to continue using the same URLs for accessing the websites - however some of the tutorials I have read suggest to use a site.local type convention, but I would prefer not to.

Anybody got any suggestions how to make this work?

-

migrated from stackoverflow.comDec 7 '11 at 0:24

This question came from our site for professional and enthusiast programmers.

The ideal way of dealing with this is to create multiple vhosts - one for each site. It might be worthwhile to take a one time penalty of changing URLs and changing the way you're used to accessing them. This will definitely save you a lot of time & trouble if you continue using this setup for multiple projects. This looks like a development setup, so it'd also help mimic production behavior.

-

You're probably looking for something like mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html with RewriteBase.

-

I am with skjaidev in that matter. The best and easiest way for you, should be to setup vhosts. Which is fairly simple. All you need to do is:

1.) Deactivate the ServerRoot and DocumentRoot directives in the httpd.conf (or apache.conf, or however the master configuration file is called on your system). Deactivate means either delete the line, or preferably put a '#' infront of it.

2.) Deactivate the corresponding <Directive...> container. You can find them as they have the same path as your DocumentRoot.

Those two steps are not required, but i do it this way for some reasons

3.) Include a new file called vhost.conf or use the one offered by your installation. You can achieve this by adding the following line in your httpd.conf

Include "C:\Path\to\apache2\confdir\vhost.conf"


4.) Add NameVirtualHost *:80 on top of the vhost.conf (exactly one time) with 80 being the port you want the apache to listen to

5.) Add a <VirtualHost ...> container like this.

<VirtualHost *:80>
ServerName site1.local
DocumentRoot "C:\Users\GSTAR\Documents\Web Sites\Site1\"
DirectoryIndex index.html index.php
<Directory />
AllowOverride None
</Directory>

<Directory "C:\Users\GSTAR\Documents\Web Sites\Site1\">
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>


This is just an example configuration. You have to adjust this to fit your needs.

6.) Add as many <VirtualHost ...> containers in above's style as you want. Just adjust the DocumentRoot and Directory directives

7.) To make the site1.local and site2.local work you will have to either edit your hosts file, or modify your local DNS server. In case you are using Windows 7, you can access this file by right clicking your preferred editor and start it with administrator rights. If you don't, you can't edit it. Now you open it by browsing to C:\Windows\system32\drivers\etc\hosts