Laravel always returns 404

I am using Laravel as backend (REST API) and AngularJS as front end (consumer API). I would like to have redirects:

/api --> /backend/public (Laravel)
/    --> /frontend/app   (AngularJs)

      

Frontend works without issue, but Laravel always returns 404 for existing routes (NotFoundHttpException in RouteCollection.php).

Where am I going wrong?

My folder structure:

/var/www
-- .htaccess (1)
-- frontend
---- app
------ index.html
------ .htaccess (2)
-- backend
---- public
------ index.php
------ .htaccess (3)

      

.htaccess (1)

<IfModule mod_rewrite.c>
   <IfModule mod_negotiation.c>
      Options -MultiViews
   </IfModule>

   RewriteEngine On

   # Redirect Trailing Slashes...
   RewriteRule ^(.*)/$ /$1 [L,R=301]

   # Backend
   RewriteRule ^api(.*) backend/public/$1 [L]

   # Frontend
   RewriteRule ^(.*) frontend/app/$1 [L]
</IfModule>

      

.htaccess (2)

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [L]
</IfModule>

      

.htaccess (3)

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteBase /api/

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

      

Apache configuration

<VirtualHost *:80>
    ...
    DocumentRoot /var/www/
    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    ...
</VirtualHost>

      

Part backend/app/Http/routes.php

<?php

Route::get('/', function()
{
    return 'Hello World';
});

Route::get('/test', function()
{
    return 'Hello Test';
});

      

Every request to backend ( http://domain.com/api*

) returns NotFoundHttpException in RouteCollection.php line 145

from Laravel (this is how it works backend/public/index.php

), for example:

http://domain.com/api
http://domain.com/api/test

      

Thank you for your time.

+3


source to share


1 answer


You need to move the routes into a route group with a prefix api

. For example:

Route::group(['prefix' => 'api'], function(){
    Route::get('/', function()
    {
        return 'Hello World';
    });

    Route::get('/test', function()
    {
        return 'Hello Test';
    });
});

      



The reason is that your Laravel application is not served in the root domain, but inside a child folder. Therefore, the URI of each request starts with api

.

+1


source







All Articles