Skip to content

Instantly share code, notes, and snippets.

@CrankyNVGuy
Forked from ukazap/vhosts.bat
Created February 25, 2018 22:42
Show Gist options
  • Save CrankyNVGuy/ca8554bfc16b605e9c9623a4e518dac0 to your computer and use it in GitHub Desktop.
Save CrankyNVGuy/ca8554bfc16b605e9c9623a4e518dac0 to your computer and use it in GitHub Desktop.
Windows batch script for creating new virtual host entries on Apache.
@echo off
rem adapted to use with XAMPP on Windows to enable virtual hosts.
rem -----Settings you can safely modify-----
set texteditor=notepad
set hostsfile=C:\Windows\System32\drivers\etc\hosts
set vhostsfile=C:/xampp/apache/conf/extra/httpd-vhosts.conf
set htdocs=C:/xampp/htdocs
set htdocsdos=C:\xampp\htdocs
rem -----End of Settings you can safely modify-----
rem ----- Probably best to leave everything below here alone unless you know what you are doing -----
set version=1.21
if ["%1"] == ["edit"] goto:edit
if ["%1"] == ["add"] goto:add
goto:usage
:add
if ["%2"] == [""] echo Insufficient parameter :-( & goto:usage
:addresolver
rem Add virtualhost to local resolver
>nul find "%2" %hostsfile% && (
echo %2 is already in %hostsfile%
goto:addfolder
) || (
rem not in hosts file, add it
echo adding %2 to %hostsfile%
echo.127.0.0.1 %2 www.%2 >> %hostsfile%
)
:addfolder
rem add directory structure to htdocs
if exist "%htdocsdos%\vhosts\%2\public" goto:folderexists
if not exist "%htdocsdos%\vhosts\" md "%htdocsdos%\vhosts"
if not exist "%htdocsdos%\vhosts\%2" md "%htdocs%\vhosts\%2"
if not exist "%htdocsdos%\vhosts\%2\logs" md "%htdocs%\vhosts\%2\logs"
if not exist "%htdocsdos%\vhosts\%2\private" md "%htdocs%\vhosts\%2\private"
if not exist "%htdocsdos%\vhosts\%2\public" md "%htdocs%\vhosts\%2\public"
echo %htdocsdos%\vhosts\%2\public has been created
goto:addindex
:folderexists
echo %htdocsdos%\vhosts\%2\public already exists
:addindex
rem add index.php to public folder
set indexfile=%htdocsdos%\vhosts\%2\public\index.php
echo. ^<!DOCTYPE html^> >> %indexfile%
echo. ^<html^> >> %indexfile%
echo. ^<head>^ >> %indexfile%
echo. ^<title^>SUCCESS!^</title^> >> %indexfile%
echo. ^</head^> >> %indexfile%
echo. ^<body^> >> %indexfile%
echo. ^<?php echo '^<center^> Virtual host %2 is working.^<br^>'; ?^> >>%indexfile%
echo. ^<?php echo 'Path: "%htdocsdos%\vhosts\%2\public" ^</center^> '; ?^> >>%indexfile%
echo. ^</body^> >> %indexfile%
echo. ^</html^> >> %indexfile%
:fixdefault
rem Make sure default XAMPP page works now that we have set up vhosts
>nul find "##Begin Host: XAMPP Defaults" %vhostsfile% && (
goto:addvhost
) || (
echo. >> %vhostsfile%
echo.##Begin Host: XAMPP Defaults >> %vhostsfile%
echo.##This section is to make sure XAMPP default page opens as default >> %vhostsfile%
echo.^<VirtualHost *:80^> >> %vhostsfile%
echo. DocumentRoot "%htdocs%" >> %vhostsfile%
echo. ServerName localhost >> %vhostsfile%
echo.^</VirtualHost^> >> %vhostsfile%
echo. >> %vhostsfile%
echo.##End Host: XAMPP Defaults >> %vhostsfile%
)
:addvhost
rem Add virtualhost to vhost file
>nul find "%2" %vhostsfile% && (
echo %2 is already in %vhostsfile%
goto:notadded
) || (
rem not in vhost config, add it
echo. >> %vhostsfile%
echo.## Begin Host: %2 >> %vhostsfile%
echo.^<VirtualHost *:80^> >> %vhostsfile%
echo. ServerAdmin webmaster@%2 >> %vhostsfile%
echo. ServerName %2 >> %vhostsfile%
echo. ServerAlias www.%2 >> %vhostsfile%
echo. DocumentRoot "%htdocs%/vhosts/%2/public" >> %vhostsfile%
echo. ErrorLog "%htdocs%/vhosts/%2/logs/error.log" >> %vhostsfile%
echo. CustomLog "%htdocs%/vhosts/%2/logs/access.log" common >> %vhostsfile%
echo. ^<Directory "%htdocs%/vhosts/%2/"^> >> %vhostsfile%
echo. Options Indexes FollowSymLinks >> %vhostsfile%
echo. AllowOverride All >> %vhostsfile%
echo. Order allow,deny >> %vhostsfile%
echo. Allow from all >> %vhostsfile%
echo. ^</Directory^> >> %vhostsfile%
echo.^</VirtualHost^> >> %vhostsfile%
echo.## End Host: %2 >> %vhostsfile%
echo. >> %vhostsfile%
)
goto:done
:done
echo.%2 has been added!
echo.Restart Apache, then open http://%2/index.html in a web browser to test.
goto:eof
:notadded
echo.%2 appears to already be provisioned.
goto:eof
:edit
if ["%2"] == [""] echo Insufficient parameter :-( & goto:usage
if ["%2"] == ["v"] echo Editing httpd-vhosts.conf & %texteditor% %vhostsfile% & goto:eof
if ["%2"] == ["h"] echo Editing hosts & %texteditor% %hostsfile% & goto:eof
goto:eof
:usage
echo.
echo.add-vhost for XAMPP version %version%
echo.Usage:
echo. vhosts add yourvirtualhost.com (adds a virtualhost)
echo. vhosts edit v (edit vhost config file)
echo. vhosts edit h (edit hosts file)
echo.
echo. NOTE: Works best if you open command prompt as Administator before you run this!
echo. Much of what happens needs elevated privileges, even if you *are* the admin.
echo.
:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment