Adding Apache VirtualHost, DocumentRoot of localhost changes to the VirtualHost's DocumentRoot

Jigar D picture Jigar D · Mar 5, 2012 · Viewed 16.3k times · Source

Everything is working perfectly in my Windows 7.

The problem is when I add a domain1.com as VirtualHost, localhost's DocumentRoot changes to VirtualHost's DocumentRoot.

Eg: When I visit http://localhost, DocumentRoot which I specified for domain1.com is opened and not the one specified in httpd.conf.

My httpd-vhosts.conf file is:

NameVirtualHost 127.0.0.1:80
NameVirtualHost domain1.com:80
<VirtualHost domain1.com:80>
<Directory "e:/program files/apache/htdocs/domain1.com">
    Options FollowSymLinks Indexes
    AllowOverride All
    Order deny,allow
    allow from All
</Directory>
ServerName domain1.com
ServerAlias domain1.com
ScriptAlias /cgi-bin/ "e:/program files/apache/htdocs/domain1.com/cgi-bin/"
DocumentRoot "e:/program files/apache/htdocs/domain1.com"
ErrorLog "E:/Program Files/apache/logs/domain1.com.err"
CustomLog "E:/Program Files/apache/logs/domain1.com.log" combined
</VirtualHost>

My Hosts file :

127.0.0.1   domain1.com

My httpd.conf file :

DocumentRoot "e:/program files/apache/htdocs"
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>
<Directory "e:/program files/apache/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

I know one solution is to add VirtualHost with ServerName as 127.0.0.1 or localhost but I am avoiding that.

Can I make localhost's DocumentRoot specified in httpd.conf a default one ?

Please let me know if you need additional information.

Thanks, Jigar.

Answer

Herokiller picture Herokiller · Dec 12, 2012

as stated in https://httpd.apache.org/docs/2.2/vhosts/name-based.html

"If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host."