Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaghadon's avatar

api routes not working on live server

the api routes in my laravel project works fine in localhost, when it is served by php server from localhost. But it doesn't work on the live server when it is served by apache in the live server.

When i try to do this, it throws me a 404 error (Page not found).

Please how do you advice i solve this?

0 likes
24 replies
vibe's avatar

Change the folder Structure It helps most of the time

Sinnbeck's avatar

Can you show one of the methods that fails? And the route

If you have a firstOrFail without exception handling it could give you issues

zaghadon's avatar

I think it is a configuration issue, if you have deployed laravel on apache before, please advice on how api routes can be directed to routes/api.php instead of web.php becasue that's what i think is the cause

Sinnbeck's avatar

Which laravel version are you using? You can just check your RouterServiceProvider. It should have the api route handler

zaghadon's avatar

it is deployed on a vps server, an ubuntu 18.04 server running apache and php, how do I change the folder structure???

zaghadon's avatar

Sir @sinnbeck the thing i that it works properly as intended on localhost, when I run php artisan serve and open localhost:8000/api/route but it shows page not found error handler when I open www.projectx.com/api/route using postman

it's running Laravel 5.8

Sinnbeck's avatar

I assume that means that web routes work on the server? Can you run php artisan route:list on the server? Does it list the api routes?

zaghadon's avatar

yes web routes runs on server. but api routes doesn't work

but on localhost when I serve using php artisan serve, both works properly

Sinnbeck's avatar

Did you run php artisan route:list to check if they are listen?

If they are, can you post one of those that does not work?

zaghadon's avatar

the route:list command doesn't work on my project including route:cache because a lot of the project codebase doesn't follow some standard laravel conventions

Sinnbeck's avatar

That could make it quite hard for us to debug here sadly.

Can you try posting your api.php file? Or at least some of it. Is there auth of some kind on all routes? If not can you open the page in a regular browser?

zaghadon's avatar

but the web routes runs properly on the server, on the api routes doesn't work, both of them works on the localhost. do you think it has got to do with .htaccess??? or apache configuration probably???

In my mind I'm thinking what's the correct way to configure apache rewrite base??? do you think this might be the cause??? what exactly could be wrong???

zaghadon's avatar

on some routes in the api the is auth:api middleware but the routes I'm visiting has no auth.

zaghadon's avatar
<?php

use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Carbon\Carbon;
use \App\Jobs\SendCashCodeSMS;


/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/* Route::middleware('auth:api')
->get('/user', function(Request $request) {
return $request->user();
}); */
    Route::group(['prefix' => 'ussd'], function(){
            Route::post('cashurban', 'USSDController@cashurban');
            Route::get('transferpay', 'USSDController@transferpay');
            Route::get('cashout', 'USSDController@cashout');
            Route::get('ussdtransaction', 'USSDController@ussdtransaction');
            Route::get('validatecharge', 'USSDController@validateCharge');
            Route::post('/rave/callback', 'flutterwaveController@callback')->name('callback');
    });

    Route::group(['prefix' => 'pos'], function(){

        Route::post('register-agent', 'POSController@MerchantOnboarding')->name('merchant_onboarding');
        Route::post('agent-terminal', 'POSController@POSterminal')->name('pos_terminal');
        Route::post('transaction/callback', 'POSController@CgateCallback')->name('pos_callback');
    });

zaghadon's avatar

yes, my .htaccess is the default htaccess, and I don't have any folder api in the public/ directory

Sinnbeck's avatar

You have a lot of unused use statements in the file. Try removing them. (all of them)

Did you remember to run composer install on the server?

zaghadon's avatar

yeah I have all of those things in place.

those unused statements are actually commented out.

spyworld's avatar

if you facing similar problem. please create a new thread.

rc0d3's avatar

I'm using laravel ^9.2, 2022 :p

The solution that worked for me in cent os usign httpd is from laravel 6 remove space: laravel. com/docs/6.x/installation#web-server-configuration

using the .htaccess in public folder works fine :) (without the .htacces all works, however the api routes no)

Please or to participate in this conversation.