Funnydutchman Funnydutchman - 1 month ago 8
Apache Configuration Question

Redirect sub.domain.com to /var/www/subfolder and sub.domain.com/api to /var/www/apifolder

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


And should be recursive, so
sub.domain.com/api/v1
should also go to
/var/www/api
.
Is this even possible with apache?

Any suggestions to make this less confusing would be appreciated ;p

Answer

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.

Comments