我试图将一个虚拟主机映射到另一个虚拟主机的子目录,例如http://host2.com -> http://host1.com/host2 。 此时,无论何时访问http://host2.com ,都会映射到http://host1.com而不是http://host1.com/host2
我的默认站点文件是这样的
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName "host1.com" <Directory /srv/www/host1> Options Indexes FollowSymLinks MultiViews AllowOverride None Order deny,allow Allow from all </Directory> DocumentRoot /srv/www/host1 WSGIScriptAlias / /srv/www/host1/apache/django.wsgi </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@localhost ServerName "host2.com" DocumentRoot /srv/www/host1 <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://host1.com/host2 ProxyPassReverse / http://host1.com/host2 </VirtualHost>
我错过了什么? 我不知道是否应该重要,但我使用的是与wsgi的Django。
这实际上可以通过使用ServerAlias指令使用单个<VirualHost />完成。 然后,您可以使用RewriteRule将这些请求传递给正确的目录,并通过closures[R]来重写请求,从而保持URL不变。
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName host1.com ServerAlias host2.com <Directory /srv/www/host1> Options Indexes FollowSymLinks MultiViews AllowOverride None Order deny,allow Allow from all </Directory> DocumentRoot /srv/www/host1 WSGIScriptAlias / /srv/www/host1/apache/django.wsgi RewriteEngine On RewriteCond %{HTTP_HOST} host2\.com [NC] RewriteRule (.*) /host2$1 [L] </VirtualHost>
希望这可以帮助。
这里有另一个可能的帮助: Redir
我没有testing下面,但你明白了。
你可以使用redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.host2\.com [NC]
RewriteRule ^(.*)$ http://host1\.com\host2 [L,R=301]