Page 1 of 1

Some helpful things I've learned about Virtual Hosts

PostPosted: 14. April 2007 20:35
by april
I've been struggling with setting up multiple Virtual Hosts on Windows for a few months and just solved my problem. I'm betting a bunch of you are hitting this same problem, so here's what I've found that works:

If your using <Directory> You absolutely MUST define this before you add individual virtual hosts. You can do it like so:
Code: Select all
<Directory C:\web>
  Order Deny,Allow
  Allow from all
</Directory>


The directory path is for a single folder (in this example "web")that will contain all subsequent virtual host folders. So if I created a domain named test.com in my hosts file, I will place it inside C:\web\test.com when I set up my virtual host's DocumentRoot.

Not doing this will result in localhost being overridden by the virtual host. Also make sure you go through httpd.conf and check the paths Apache has set for <Directory>. They should point to xampp's htdocs folder always:

Code: Select all
<Directory "C:/Program Files/xampp/htdocs">


Make sure you uncomment the line in httpd.conf that allows Apache to load the separate virtual hosts file. The line should look like this:

Code: Select all
# Virtual hosts
Include conf/extra/httpd-vhosts.conf


Your httpd-vhosts.conf contains all your virtual host settings. It should be structured something like this:

Code: Select all
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host.example.com
##    DocumentRoot /www/docs/dummy-host.example.com
##    ServerName dummy-host.example.com
##    ServerAlias www.dummy-host.example.com
##    ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common
##</VirtualHost>

##<VirtualHost *:80>
##    ServerAdmin webmaster@dummy-host2.example.com
##    DocumentRoot /www/docs/dummy-host2.example.com
##    ServerName dummy-host2.example.com
##    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
##    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
##</VirtualHost>
<Directory C:\web>
  Order Deny,Allow
  Allow from all
</Directory>

## test.com
<VirtualHost *:80>
ServerAdmin admin@test.com
DocumentRoot C:\web\test.com\htdocs
ServerName test.com
ServerAlias test.com
   <Directory C:\web\test.com\htdocs>
      Options Indexes FollowSymLinks ExecCGI Includes
      AllowOverride All
      Order allow,deny
      Allow from All
   </Directory>
</VirtualHost>

## test2.com
<VirtualHost *:80>
ServerName test2.com
ServerAlias test2.com
DocumentRoot C:\web\test2.com\htdocs
ServerAdmin admin@test2.com
   <Directory "C:\web\test2.com\htdocs">
      Options Indexes FollowSymLinks ExecCGI Includes
      AllowOverride All
      Order allow,deny
      Allow from All
   </Directory>
</VirtualHost>


Notice I made and extra folder ("htdocs") below the folder with the domain name. I've found it easier to store different types of files (PHP, javascript, ect.) inside separate folders within the domain. So C:\web\test2.com\htdocs holds html, C:\web\test2.com\css holds css files, etc. The DocumentRoot doesn't have to be the folder with the domain name in it; by setting the DocumentRoot to C:\web\test2.com\htdocs it will open test.com as the path C:\web\test2.com\htdocs. This makes it easy to create relative links that cut down on work when transferring a site from one domain to another.

PostPosted: 16. April 2007 21:21
by april
Not doing this will result in localhost being overridden by the virtual host.

Apparently I spoke too soon. The fact is that localhost is going to be overridden the moment you make a virtual host. This simple solution to get it back is just to create a virtual host with a path back to xampp's htdocs folder and name it localhost.

This makes a lot of sense but I haven't heard very many people explain this when they talk about setting up virtual hosts.

PostPosted: 28. June 2007 18:31
by trakkstar
Wow, thank goodness I found your post, April. I've been struggling with getting SSI to work specifically in my virtual hosts. Kicking myself for not noticing that I did not include INCLUDES in my Options statement IN THE VIRTUAL HOST FILE. :P

Glad I saw your post.

I am new here

PostPosted: 27. November 2008 17:02
by googleseo
Hi,
I am new to all here and I bet you can teach some startup guide into mod rewrite rules and most basic apache mods.