3

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?

1
  • I think to find the answer you should not use the phrase "virtualhost", but look into reverse proxies.
    – jiggunjer
    Apr 7, 2020 at 18:39

3 Answers 3

2

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:

  • If the server1 virtualhost has a separate hostname, you could have a redirect
  • If all you want is to serve content from a different path for that URL, you can use an AliasMatch to map that path
  • You could use mod_rewrite to do an URL rewrite according to your specification

I 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.

1
  • Thanks! Apologies for being unable to give you an upvote, it says I don't have enough points yet. Dec 27, 2013 at 21:37
1

Sounds like this could be a job for Apache's proxy module.

Check out the ProxyPass directive:

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass

-1

Virtualhosts are for dealing with the domain part of the request so that you can host multiple domains on the same server.

you could possibly do something with an alias or rewriting. or you could build the different urls into your app and let it deal with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.