Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache: Virtual Host configuration

As i tried to configure my virtual host in apache. I put something like this,

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

And in my hosts file i put something like this,

127.0.0.1       localhost
127.0.0.1       gift.loc

And i run it on the browser,

http://gift.loc - is fine

But when i tried using this,

http://localhost/othersite - can't found

Do i missed somehting to configure? ANy ideas...

Thanks in advance,

like image 391
Trez Avatar asked Sep 06 '25 03:09

Trez


2 Answers

You need a VirtualHost entry for every host you want apache to handle. The first one in the config file will be used as the default if no other VirtualHosts match the request.

For example if we have:

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /example/htdocs/gift
   ServerName example.com  
</VirtualHost>

A request for foobar.org will get handled by the gift.loc virtual host.

like image 79
speshak Avatar answered Sep 07 '25 22:09

speshak


you need to put localhost in the vhosts.conf

    NameVirtualHost *:80

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/
       ServerName localhost
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/gift
       ServerName gift.loc  
    </VirtualHost>

This works fine (Make sure you restart apache). If you need to check your configuration you can (on linux at least) run httpd -S.

like image 37
miller the gorilla Avatar answered Sep 07 '25 21:09

miller the gorilla