在 第一个例子 中说过,转为基于 IP 地址的虚拟主机设置很容易做到。但不幸的是,那种做法并不高效,因为这样会在每次处理请求时,需要查询 DNS 。 通过在文件系统中包含IP 地址的做法可以避免这样的问题。这样一来, 免去了和服务器名字的关联,在日志记录中也一样可以用 IP 来分离不同日志。 Apache 将不会为了确定服务器名字(server name)而去做 DNS 查询。
<Directory /www/hosts>
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
</Directory>
# 接下来是关键部分
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all RewriteMap lowercase int:tolower
## 首先处理普通文档:
# 允许变名 /icons/ 起作用 - 其他变名类同
RewriteCond %{REQUEST_URI} !^/icons/
# 允许 CGIs
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# 开始“变戏法”
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1