Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks! Apologies for being unable to give you an upvote, it says I don't have enough points yet. –  user2242700 Dec 27 '13 at 21:37

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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