在Windows環境中建立虛擬主機是一件很麻煩的事。您必須建立網站資料夾,編輯 httpd.conf、主機等等。為了自動化其中一些工作,我決定嘗試建立一個批次文件來自動化其中一些工作。這是我為初學者想出來的:
@ECHO OFF
::location of vhosts
SET BASEURL=D:\web\vhosts\
:sitenameprompt
::ask for a sitename
set /p sitename="Enter Sitename: " %=%
echo Before IF
IF EXIST %BASEURL%%sitename% echo The specified site folder already exists! Choose another.
IF EXIST %BASEURL%%sitename% goto sitenameprompt
IF NOT EXIST %BASEURL%%sitename% echo Creating folder
IF NOT EXIST %BASEURL%%sitename% MD %BASEURL%%sitename%
echo Adding virtualhost to httpd.conf
pause
(
echo ###%sitename%###
echo <VirtualHost *:80>
echo ServerAdmin admin@%sitename%
echo DocumentRoot "%BASEURL%%sitename%"
echo ServerName %sitename%.dev
echo ServerAlias %sitename%.dev
echo <Directory "%BASEURL%%sitename%">
echo Options Indexes FollowSymLinks Includes ExecCGI
echo Order allow,deny
echo Allow from all
echo </Directory>
echo </VirtualHost>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf
pause
echo Restarting apache
::httpd -k restart
現在,當執行此腳本時,它會在 httpd.conf 部分給出語法錯誤。請記住,我對此還很陌生,所以這可能是顯而易見的事情。那我哪裡搞砸了?
答案1
醜陋的符號<>|&
必須用插入符號批量轉義^
,試試這個:
@echo off
(
echo ###%sitename%###
echo ^<VirtualHost *:80^>
echo ServerAdmin admin@%sitename%
echo DocumentRoot "%BASEURL%%sitename%"
echo ServerName %sitename%.dev
echo ServerAlias %sitename%.dev
echo ^<Directory "%BASEURL%%sitename%"^>
echo Options Indexes FollowSymLinks Includes ExecCGI
echo Order allow,deny
echo Allow from all
echo ^</Directory^>
echo ^</VirtualHost^>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf