3

I use Laravel as backend (API REST) and AngularJS as frontend (consuming API). I would like to have redirects:

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

Frontend runs without problem, but Laravel always return 404 for existing routes (NotFoundHttpException in RouteCollection.php).

Where I make a mistake?

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 of 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*) return NotFoundHttpException in RouteCollection.php line 145 from Laravel (so backend/public/index.php is running), for example:

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

Thank you for your time.

3
  • Maybe turn rewrite debug logging on so you can see what's going on? But, assuming the .htaccess rewrites are all working properly, the issue is possibly that your URLs will appear to Laravel prefixed with api/ and your routes file doesn't contain these prefixes? Commented May 19, 2015 at 9:52
  • Do you have any route groups with the prefix attribute? After redirected by htaccess, the URI of the request will be changed and sometime you registered wrong route. Can you post your test URL and the part of the routes.php file that register that route? I'll deleted my old answer and continue testing your case if you provide more data. Commented May 19, 2015 at 11:12
  • @hieu-le Thanks, I updated my problem description above. Commented May 19, 2015 at 11:30

1 Answer 1

1

You need wrap your routes inside a route group with a prefix of api. For example:

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

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

The reason is your Laravel application is not served at root domain but inside a child folder. Therefore, the URI of each request will start by api.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it's work! Thanks! I thought group isn't necessary since I use in .htaccess (3) rule RewriteBase /api/. Now I add group prefix and remove RewriteBase /api/ from .htaccess (3). This is the best solution? I'm asking because now api prefix is define twice time: in .htaccess (1) and in backend/app/Http/routes.php. There isn't any solution to define prefix only in .htaccess (1)?
I'm afraid that we cannot remove the group from routes.php file. The RewriteBase does not strip the URI prefix. You can safely remove the RewriteBase from your .htaccess (3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.