I want to redirect the /api folder to a new documentRoot (new virtualhost) from the already existing virtualhost
domain.com -> /var/www/domain
sub.domain.com -> /var/www/sub
sub.domain.com/api -> /var/www/api
As already said above in the comments you can simply define virtual hosts for this. You do not need any redirection commands at all:
Listen 80 <VirtualHost *:80> ServerName domain.com DocumentRoot /var/www/domain <Directory /var/www/domain> # ... </Directory> </VirtualHost> <VirtualHost *:80> ServerName sub.domain.com Alias /api /var/www/api/index.php <Directory /var/www/api> # ... </Directory> DocumentRoot /var/www/sub <Directory /var/www/sub> # ... </Directory> </VirtualHost>
This obviously assumes that your
API is implemented by a script called
/var/www/api/index.php. You certainly can save that implementation as whatever you like, for example
/var/www/api. However that does not look very realistic to me, APIs are typically implemented on a scripting base and scripts usually require include files, so a folder holding the implementation certainly does make sense. And I doubt you want to throw all those files into
/var/www, so I assume that
/var/www/api is meant to hold the implementation.
I tested this setup locally, it does work for me.