Is it possible to configure Apache to have different virtual hosts listening on the same IP address, same port, such that certain paths (not the full domain, just the local path) will direct to one virtual host, and others will default to the other?
For example:
#All requests to www.mysite.com/special-url go here
<VirtualHost /special-url:80>
DefaultType application/x-httpd-php
ServerName server1
Document Root "/home/me/server1"
<Directory "/home/me/server1">
AllowOverride None
Order allow, deny
allow from all
</Directory>
</VirtualHost>
#All other requests go here
<VirtualHost *:80>
DefaultType application/x-httpd-php
ServerName server1
Document Root "/home/me/server2"
<Directory "/home/me/server2">
AllowOverride None
Order allow, deny
allow from all
</Directory>
</VirtualHost>
This server configuration doesn't work, and neither does adding wildcard combinations to the first VirtualHost
tag (e.g. VirtualHost */special-url:80
). Instead, when I run this, Apache directs requests to /special-url
to /home/me/server1
. But if I look for /foo
it will still look for that in server1
and return 404
even if there is a directory called foo
in /home/me/server2
.
Is there any way to have one VirtualHost just for certain URLs, and another VirtualHost for all other URLs?
Best Answer
That's not how it works. A virtual host is based on either IP address or on hostname, not on path. But there are a few other things you can do to achieve the same objective:
server1
virtualhost has a separate hostname, you could have a redirectAliasMatch
to map that pathmod_rewrite
to do an URL rewrite according to your specificationI can't be more specific than that without knowing more about what exactly you want to achieve. But I believe the Apache documentation on mapping URLs to file system will give you a good starting point.