using virtual host in apache like virtual directory in windows

Apache No Comments »

by default we have our files stored in a user defined path like d:\www\xampp\htdocs\site1
when we access we give as localhost/site1

using virtual host option you can use like the following
consider you have your site in d:\www\xampp\htdocs\site2
you can access your site as “http://site2″ or “http://site2.com” or “http://site2.local” … by updating the httpd.conf file
which will be present in the apache/conf folder of your installation.

edit your httpd.conf file and go to the bottom of it. and you can add like the following.
i assume that your default document root is d:\www\xampp\htdocs

NameVirtualHost 127.0.0.1
 
<VirtualHost 127.0.0.1>
   DocumentRoot "d:\www\xampp\htdocs\site1"
   ServerName site1
</VirtualHost>

before using this you have to do two steps

one is restarting apache and the other is as follows

since we are going to give http://site1 windows will connect to the internet to fetch the site but we have to inform windows
not to connect to the www and instead connect to the local site1. for this you have to add the site1 in a windows file which
is stored in the following folder

C:\WINDOWS\system32\drivers\etc

the file name is “hosts” without extension

you have to add the following line in the file “hosts”

127.0.0.1       site1 or
127.0.0.1       site1.com [if you want to access that way] or any of your wish

consider that you are having your site outside the default path of apache say d:\mysite
then you have to follow the steps below to include that

<Directory "D:\mysite">
  Order Deny,Allow
  Allow from all
</Directory>
 
and this besides the similar code
 
<VirtualHost 127.0.0.1>
   DocumentRoot "d:\mysite"
   ServerName mysite
</VirtualHost>

include the above better before NameVirtualHost 127.0.0.1

so you have two sites now and you can run your sites like the following
type in the address bar after restarting apache as “site1″ which will automatically be called as “http://site1″ and mysite whic will be like “http://mysite”

try it and enjoy…

Entries RSS