I’m continually adding new sites to my PC’s hosts file and to Apache’s httpd-vhosts.conf file. I’ve made a command line script (batch file), to automate this process. The script assumes the following:
  • The command line prompt is running with administrative privileges
  • Your hosts file is located at: C:\Windows\System32\drivers\etc
  • Your localhost environment is Apache based
  • Have an existing environment variable called Apache that points to where Apache resides (such as:
  • C:\Xampp\Apache)
  • You use httpd-vhosts.conf in a similar manner
The actual script:

cd C:\Windows\System32\drivers\etc
if exist "hosts.bak" del hosts.bak
copy hosts hosts.bak

echo 	127.0.0.1	localhost	%3.localhost >>C:\Windows\System32\drivers\etc\hosts

cd %ApachePath%\conf\extra
if exist "httpd-vhosts.conf.bak" del httpd-vhosts.conf.bak
copy httpd-vhosts.conf httpd-vhosts.conf.bak
echo # %3 >>httpd-vhosts.conf
echo <VirtualHost %3.localhost:80> >>httpd-vhosts.conf
echo 	DocumentRoot "%ProjectsPath%/%1/%2/Src/Web" >>httpd-vhosts.conf
echo 	ServerName %3.localhost >>httpd-vhosts.conf
echo 	ErrorLog "logs/%3.localhost-error.log" >>httpd-vhosts.conf
echo 	CustomLog "logs/%3.localhost-access.log" combined >>httpd-vhosts.conf
echo 	Options Indexes FollowSymLinks Includes ExecCGI >>httpd-vhosts.conf
echo 	AcceptPathInfo On >>httpd-vhosts.conf
echo 	<Directory "%ProjectsPath%/%1/%2/Src/Web"> >>httpd-vhosts.conf
echo 		Options All >>httpd-vhosts.conf
echo 		AllowOverride All >>httpd-vhosts.conf
echo 		Require all granted >>httpd-vhosts.conf
echo 		Allow from 127.0.0.1 >>httpd-vhosts.conf
echo 	</Directory> >>httpd-vhosts.conf
echo </VirtualHost> >>httpd-vhosts.conf