前提:有的时候我想让一个网站上实现多个二级域名指向网站的多个功能菜单,这样就需要多个域名绑定网站下的多个子目录。默认只有一个域名绑定到一个IP地址,指向apache网站的根目录。

好比我有三个域名:

网站域名:www.test.com (在万网或者新网里面)

功能1域名:gongneng1.test.com

功能2域名:gongneng2.test.com


1、我们先在万网或者新网域名解析中添加A记录,将三个域名分别指向到同一个IP地址如:192.168.1.100;

2、在apache根目录(比如:/usr/local/apache/htdocs)里面新建两个文件夹分别为gongneng1和gongneng2;

3、这步是最关键的:在httpd.conf最下面添加以下内容


<VirtualHost www.test.com:80>

ServerAdmin xxx@qq.com

DocumentRoot "/usr/local/apache/htdocs"

ServerName www.test.com

ErrorLog logs/www_error_log.txt

CustomLog logs/www_log.txt common

</VirtualHost>


<VirtualHost gongneng1.test.com:80>  

ServerAdmin xx@qq.com  

DocumentRoot "/usr/local/apache/htdocs/gongneng1"  

ServerName gongneng1.test.com  

ErrorLog logs/gongneng1_error_log.txt  

CustomLog logs/gongneng1_log.txt common  

</VirtualHost>  


<VirtualHost gongneng2.test.com:80>

ServerAdmin xx@qq.com

DocumentRoot "/usr/local/apache/htdocs/gongneng2"

ServerName gongneng2.shyygame.com

ErrorLog logs/gongneng2_error_log.txt

CustomLog logs/gongneng2_log.txt common

</VirtualHost>


4、重启apache

  /usr/local/apache/bin/apachectl restart