# 首先把机器名改为小写,添加到请求的文件路径前,并继续处理
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
# 重写请求
RewriteRule ^([a-z0-9])([a-z0-9])([a-z0-9])\.home\.com([\.]*)/(.*) /member/$1$2/$2$3/$1$2$3/$5
RewriteRule ^([a-z0-9])([a-z0-9])([a-z0-9]*)([a-z0-9])([a-z0-9])\.home\.com([\.]*)/(.*) /member/$1$2/$4$5/$1$2$3$4$5/$7
---------------------------------------------------------------------------------
优点:只要一个IP地址就可以提供大量的虚拟主机服务,维护方便。
缺点:安全性低。上例为了增强安全性,禁止运行用户自己的CGI程序。
2.5 Apache WWW服务器下的重定向虚拟主机设置
重定向虚拟主机是指仅仅提供重定向功能,而不实际存放用户主页数据的虚拟主机。比如,某站点有多个镜像服务器,假设为www1.user.com、www2.user.com,…wwwn.user.com。这些服务器放在不同的网络上,分布于各地的客户访问这些服务器的速度也互不相同。而站点www.home.com通过虚拟域名user.home.com为这些镜像的服务器提供虚拟主机服务。当客户访问http://user.home.com时,www.home.com根据用户的IP地址、收集到的网络拓扑结构信息,把客户重定向到对该客户来说访问速度最快的站点。
重定向虚拟主机服务也可以通过使用URL重写功能来完成。这里仅给出简单的重写规则:
------------------------------------------------------------------------------------
RewriteRule ^([a-z0-9]+)\.home\.com([\.]*)/(.*) /www/cgi-bin/nph-redirect.cgi/$1/$3 [T=application/x-httpd-cgi,L]
------------------------------------------------------------------------------------
其中,nph-redirect.cgi为一个CGI程序,为了实现自动重定向,它需要产生完整的HTTP 应答头信息。该程序通过环境变量PATH_INFO得到用户请求的URL,通过环境变量REMOTE_ADDR得到用户的IP地址,并根据网络拓扑情况生成重定向信息,返回给客户。
参考文献:
1.Apache 1.3 User's Guide http://www.apache.org
Virtual Hosting Setup Using Apache
Abstract:
There are two types of WWW Virtual Hosting, IP-based and Name-based. Those two types are introduced in this thesis, followed by some methods about WWW Virtual Hosting configuration under Apache and the difference between them is analyzed.