Skip to content

Instantly share code, notes, and snippets.

@eagl3s1ght
Forked from CrankyNVGuy/vhosts.bat
Last active August 12, 2018 15:18
Show Gist options
  • Save eagl3s1ght/61f474cb8665e1b3ceb18d4ca826b9ed to your computer and use it in GitHub Desktop.
Save eagl3s1ght/61f474cb8665e1b3ceb18d4ca826b9ed 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 adapted to prompt for admin rights
rem adapted to allow input if no input (no cli)
rem adapted to check if settings are valid (set files exist)
@rem Check if file was executed with admin rights, else make new console admin and execute the script again
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
rem -----Settings you can safely modify-----
@rem Text editor of your choice e.g. Sublime Text (subl)
set texteditor=notepad
@rem Where Windows' hosts file is located
set hostsfile=C:\Windows\System32\drivers\etc\hosts
@rem This one depends on your installation.
set vhostsfile=C:/xampp/apache/conf/extra/httpd-vhosts.conf
@rem This one depends on your installation.
set htdocs=C:/xampp/htdocs
@rem Like the one above but with backslashes instead of forwardslashes.
set htdocsdos=C:\xampp\htdocs
rem -----End of Settings you can safely modify-----
if not exist "%hostsfile%" echo Your settings are incorrect: hostsfile (%hostsfile%) doesn't exist. & goto:eofs
if not exist "%vhostsfile%" echo Your settings are incorrect: vhostsfile (%vhostsfile%) doesn't exist. & goto:eofs
if not exist "%htdocs%" echo Your settings are incorrect: htdocs (%htdocs%) doesn't exist. & pause & goto:eofs
if not exist "%htdocsdos%" echo Your settings are incorrect: htdocsdos (%htdocsdos%) doesn't exist. & goto:eofs
rem ----- Probably best to leave everything below here alone unless you know what you are doing -----
set version=1.22
echo. Enter desired virtualhost domain:
set /p input= & goto:add
if ["%1"] == ["edit"] goto:edit
if ["%1"] == ["add"] goto:add
goto:usage
:add
if ["%2"] == [""] (
if [input] == [""] (
echo Insufficient parameter :-( & goto:usage
) else (
set answer=%input%
)
) else (
set answer=%2
)
:addresolver
rem Add virtualhost to local resolver
>nul find "%answer%" %hostsfile% && (
echo %answer% is already in %hostsfile%
goto:addfolder
) || (
rem not in hosts file, add it
echo adding %answer% to %hostsfile%
echo.127.0.0.1 %answer% www.%answer% >> %hostsfile%
)
:addfolder
rem add directory structure to htdocs
if exist "%htdocsdos%\vhosts\%answer%\public" goto:folderexists
if not exist "%htdocsdos%\vhosts\" md "%htdocsdos%\vhosts"
if not exist "%htdocsdos%\vhosts\%answer%" md "%htdocs%\vhosts\%answer%"
if not exist "%htdocsdos%\vhosts\%answer%\logs" md "%htdocs%\vhosts\%answer%\logs"
if not exist "%htdocsdos%\vhosts\%answer%\private" md "%htdocs%\vhosts\%answer%\private"
if not exist "%htdocsdos%\vhosts\%answer%\public" md "%htdocs%\vhosts\%answer%\public"
echo %htdocsdos%\vhosts\%answer%\public has been created
goto:addindex
:folderexists
echo %htdocsdos%\vhosts\%answer%\public already exists
:addindex
rem add index.php to public folder
set indexfile=%htdocsdos%\vhosts\%answer%\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 %answer% is working.^<br^>'; ?^> >>%indexfile%
echo. ^<?php echo 'Path: "%htdocsdos%\vhosts\%answer%\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 "%answer%" %vhostsfile% && (
echo %answer% is already in %vhostsfile%
goto:notadded
) || (
rem not in vhost config, add it
echo. >> %vhostsfile%
echo.## Begin Host: %answer% >> %vhostsfile%
echo.^<VirtualHost *:80^> >> %vhostsfile%
echo. ServerAdmin webmaster@%answer% >> %vhostsfile%
echo. ServerName %answer% >> %vhostsfile%
echo. ServerAlias www.%answer% >> %vhostsfile%
echo. DocumentRoot "%htdocs%/vhosts/%answer%/public" >> %vhostsfile%
echo. ErrorLog "%htdocs%/vhosts/%answer%/logs/error.log" >> %vhostsfile%
echo. CustomLog "%htdocs%/vhosts/%answer%/logs/access.log" common >> %vhostsfile%
echo. ^<Directory "%htdocs%/vhosts/%answer%/"^> >> %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: %answer% >> %vhostsfile%
echo. >> %vhostsfile%
)
goto:done
:done
echo.%answer% has been added!
echo.Restart Apache, then open http://%answer%/index.html in a web browser to test.
goto:eofs
:notadded
echo.%answer% appears to already be provisioned.
goto:eofs
:edit
if ["%2"] == [""] echo Insufficient parameter :-( & goto:usage
if ["%2"] == ["v"] echo Editing httpd-vhosts.conf & %texteditor% %vhostsfile% & goto:eofs
if ["%2"] == ["h"] echo Editing hosts & %texteditor% %hostsfile% & goto:eofs
goto:eofs
: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.
echo. Enter desired virtualhost domain:
set /p input= & goto:add
:eofs
echo.
echo.- Press any of the buttons on your keyboard to quit.
@pause>nul
goto:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment