在Apache 2.2.x上,我激活了mod_userdir 。 我使用了默认的设置,这样http://localhost/~name/ ~name/public_html/mySite http://localhost/~name/将被连接到~name/public_html/mySite ~name/public_html/并且~name/public_html/一个path,例如~name/public_html/mySite可以通过http://localhost/~name/mySite 。
我怎么能实现,通过http://mySite.name.localhost/可以达到相同的path? 我不希望像其他SF问题(如Apache:使用子域设置本地testing服务器 )中所build议的那样使用手动方法,而是希望将所有可用path自动映射到相应的URL。
我认为,需要采取以下几个步骤:更改mod_userdirconfiguration,以便localhost的子域将与机器上所有可用的用户名连接起来。 第二步可能会包括mod_rewrite的使用,以便sububdomain可以匹配到〜name / public_html中的path…
什么是你最喜欢的方式?
编辑:
解决scheme被标记。 我做了一些小修改以满足特殊要求和补充意见:
RewriteEngine on # check, if not redirected yet RewriteCond %{ENV:REDIRECT_STATUS} ^$ # look for URLS like 'subdir.username.localhost' RewriteCond %{HTTP_HOST} (.+)\.([^.]+)\.localhost$ # check if /home/username/public_html/subdir is a directory or symbolic link RewriteCond /home/%2/public_html/%1 -d [OR] RewriteCond /home/%2/public_html/%1 -l # rewrite to default target RewriteRule ^(.+) %{HTTP_HOST}$1 [C] # rewrite to /home/username/public_html/subdir RewriteRule (.+)\.([^.]+)\.localhost(?::\d+)?(.*) /home/$2/public_html/$1$3 [L]
几星期前我有类似的需求。 我解决了这个问题,使用mod_rewrite,把它放在我的默认虚拟主机定义:
RewriteEngine on # look for URLS like 'subdir.username.localhost' RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{HTTP_HOST} ([^.]+)\.([^.]+)\.localhost$ RewriteCond /home/%2/public_html/%1 -d RewriteRule ^(.+) %{HTTP_HOST}$1 [C] RewriteRule ([^.]+)\.([^.]+)\.localhost(?::\d+)?(.*) /home/$2/public_html/$1$3 [L]
还有一些在https://gist.github.com/744971更多信息,包括获取username.localhostparsing为/ home / username / public_html