|
摘自:http://wulantiankong.spaces.live ... E0F83844!1035.entry
mod_userdir可以让linux机器上的用户都有一个虚拟主机,缺省目录为~/public_html。配合suexec,其还可以使各个虚拟主机的cgi程序以各自的用户身份执行。
但是mod_userdir的形式为http://servername/~username/,而我们希望可以通过不同的域名访问不同的虚拟主机。
因此我试着使用mod_rewrite将http://domainname/的访问转换为userdir的形式。apache的配置脚本httpd.conf相关内容如下(假设用户目录为/home/username)
LogFormat \"%{VHOST}e %V %h %l %u %t %r %>s %b %{Referer}i %{User-Agent}i\" vhost
CustomLog logs/vhosts_access_log vhost
RewriteEngine on
#RewriteLog /usr/local/apache2/logs/rewrite_log
#RewriteLogLevel 9
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/home/vhost.map
RewriteCond %{ENV:VHOST} ^$
RewriteCond %{REQUEST_URI} ^/~([^/]+)
RewriteCond ${lowercase:%1} ^(.+)$
RewriteRule ^.*$ $0 [PT,E=VHOST:%1]
RewriteCond %{ENV:VHOST} ^$
RewriteCond %{HTTP_HOST} !^$
RewriteCond ${lowercase:%{HTTP_HOST}} ^(.+)$
RewriteCond ${vhost:%1} ^(.+)$
RewriteRule ^.*$ $0 [E=VHOST:%1]
RewriteCond %{ENV:VHOST} !^$
RewriteCond %{REQUEST_URI} !^/~
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteRule ^/(.*)$ /~%{ENV:VHOST}/$1 [PT]
RewriteCond %{ENV:VHOST} !^$
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(.*)$ /~%{ENV:VHOST}/$1 [PT,T=application/x-httpd-cgi]
RewriteCond %{REQUEST_URI} ^/~[^/]+/cgi-bin/
RewriteRule ^.*$ $0 [PT,T=application/x-httpd-cgi]
<Directory /home/*/public_html/>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script
AllowOverride All
Order allow,deny
Allow from all
</Directory>
vhost.map内容为
# domain name # username
example.vhost example
www.example.vhost example
erning.vhost erning
www.erning.vhost erning这样,启动apache后,就可以通过 http://www.example.vhost/ 访问 http://servername/~example/。 (设置了hosts文件,使.vhost后缀的域名可以使用) |
|