很久没用apache了(因为太膜拜nginx的缘故),最近连虚拟目录都忘了怎么配置,网上找一大堆,没解决我的问题. 我是在自己电脑上虚拟几个地址.貌似以前使用的时候(不记得什么版本了,大概1.5 1.6几的),这样写一堆: 

DocumentRoot "E:/wwwroot/yam"
ServerName   yam.com

就直接能够运行多个虚拟的地址——都成浮云了.最近装了xampp 1.7.4,几翻搜索有了一些了解. 因为是在本机运行,只有一个IP,我一开始便使用了基于域名写法:

NameVirtualHost *:80 ; 
 
ServerName www.site1.com
ServerAlias  *.site1.com
DocumentRoot E:/www/site1
<Directory "E:/www/site1">
 Options Indexes MultiViews
 AllowOverride None
 order allow,deny
 Allow from all
 
ServerName www.site2.com 
ServerAlias  www.site2.com
DocumentRoot E:/www/site2 
<Directory "E:/www/site2">
 Options Indexes MultiViews
 AllowOverride None
 order allow,deny
 Allow from all

浏览器运行文件之后发现,上面所虚拟的2个名称,指向的目录不是site1就是site2的目录(不会随机指向其中一个,而是你对配置文件稍稍修改优先级便有了变化,一直指向同一个目录). 搞得纳闷,也许遇到同样问题的人没想到一个简单的存在,hosts里面的这些域名是对应到127.0.0.1,便对两个名称使用同一<Drectory></Drectory>.如果外网访问的话我没试过,两个域名也可解析到同一IP,这样的问题是否同样会出现?如果是的话,我就正确了. 可以改成这样,便可以分别访问到了:

ServerName   yam.com
ServerAlias  yam.com
DocumentRoot E:/wwwroot
DirectoryIndex index.php default.php index.html index.htm
<Directory "E:/wwwroot">
 Options Indexes MultiViews
 AllowOverride None
 order allow,deny
 Allow from all

ServerName   chinasonic.com
ServerAlias  *.chinasonic.com
DocumentRoot E:/wwwroot/the1
DirectoryIndex index.php default.php index.html index.htm
<Directory "E:/wwwroot/the1">
 #Options Indexes MultiViews
 AllowOverride None
 order allow,deny
 Allow from all