找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 1815|回复: 0

[【推荐】] 用mod_rewrite与mod_userdir配置apache虚拟主机

[复制链接]
发表于 2009-7-30 14:38:25 | 显示全部楼层 |阅读模式
摘自: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后缀的域名可以使用)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-6-2 14:46 , Processed in 0.336717 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表