Apache vhost serving different directory for HTTP path

I have two laravel based PHP applications. One is an API server, the other a client-facing website.

To avoid cross-origin issues when doing AJAX calls, I prefer to keep the API server and the website on the same domain. I.e.:

www.domain.com/* (requests served from a website project folder)

www.domain.com/api/* (requests server from an api project folder)

For the website, I have an Apache virtual host like so:

 <virtualhost *:80="">ServerAdmin email@domain.com
     ServerName domain.com
     ServerAlias www.domain.com
     DocumentRoot /home/mainuser/www/website/public
     Options Indexes FollowSymLinks MultiViews
     <directory home="" mainuser="" www="" website="" public="">Require all granted
        AllowOverride all</directory></virtualhost> 

This works fine, but now I would like to add a special case where all requests to domain.com/api/** are served by the directory /home/mainuser/www/api/public rather than /home/mainuser/www/website/public. Can this be specified in the virtual host?

4 Replies

Yes,

You need an Alias and another Directory directive… something like this:

Alias /api /home/mainuser/www/api/public

 <directory home="" mainuser="" www="" api="" public="">[your config for this directory, e.g.:]
        Require all granted
        AllowOverride all</directory> 

Check out https://httpd.apache.org/docs/current/m … #directory">https://httpd.apache.org/docs/current/mod/core.html#directory and https://httpd.apache.org/docs/2.2/mod/mod_alias.html

I hope this helps.

Sid

Great, that seems to work for /api, but when I access /api/test, the 404 error handler of the website project is triggered, rather than the 404 error handler of the api project.

Do you have an ErrorDocument stanza anywhere in your virtualhost config? Perhaps even in a .htaccess somewhere?

I don't have a fancy error handler, so I've not come across this issue myself.

@virtualsid:

Do you have an ErrorDocument stanza anywhere in your virtualhost config? Perhaps even in a .htaccess somewhere?

I don't have a fancy error handler, so I've not come across this issue myself.

I had to add

RewriteBase /api

To the .htaccess inside /api

Now it works. But I wonder why?

My source is http://forumsarchive.laravel.io/viewtopic.php?id=14341

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct