0

I have two Laravel sites: a front page and an API site. My customer will later separate the API site on a subdomain, but for the time being the customer wants to serve the API from inside "subfolder" of the landing page (mostly because he does not have a wildcard SSL/TLS certificate yet). It should look like this:

http://example.com - front page with possibly some subroutes, such as http://example.com/contact-us

http://example.com/api/ - the API

I don't want to mix the code of both projects together; they are being maintained by different developers, so mapping the folder as an Apache VirtualHost seems the way to go.

I did as follows:

<VirtualHost *:80>
    DocumentRoot "/sites/front-page/public"

    ServerName somedomain.com
    ServerAlias www.somedomain.com

    <Directory "/sites/front-page/public">
        AllowOverride All
        Require all granted
    </Directory>

    Alias /api "/sites/api/public"

    <Directory "/sites/api/public">
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>

Now when I visit http://example.com/api/ it indeed calls index.php from the API site. But if I call http://example.com/api/some-resource it calls the index.php from the front-page site, and that fails with 404 not found.

I tried also

AliasMatch "^/api(/|$)(.*)" "/sites/api/public"

but then even http://example.com/api/ root fails with Apache 403 forbidden page (http://example.com and its routes work fine, though).

How do I tell Alias to match /api with all its subroutes and send the (.*) part to Laravel's router?

Also, I most probably will have to edit .htaccess Redirect rules in the other site to strip out the /api part from the URL before passing it to Laravel's index.php, but I'm not sure how to do it right.

In case if I'm doing it all wrong and there is another way to serve two different sites from the same domain, I'm open to suggestions.

JustAMartin
  • 231
  • 1
  • 18
  • You mention about the SSL cert at the beginning and in the first URL you have `https`, however, all the remaining URLs, including the VirtualHost, refer to `http` (port 80) - is this intentional? If you aren't using the SSL cert, then why is this an important factor to use a subdirectory? Does the API code use the document root? – MrWhite Sep 15 '17 at 13:35
  • Fixed the first URL; for the time being ignore the SSL - it will be present but is not relevant to the Alias issue. – JustAMartin Sep 15 '17 at 13:48

0 Answers0