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

Rock17's avatar

Laravel - route 404 not found - how fix it?

I am creating a mobile application that works with api on Laravel. The api should work but there are problems. I'm trying to figure it out, although I don't have previous experience with Laravel (I have experience with PHP). so on server Cpanel. when i try to call the api i get an error

 404 Not Found

a few details:

1 the api is in the public_html folder, some folders inside -app, routers, bootstrap

2 when I added file public_htm/index.php - any request to api returns that file

Any advice - I will be very grateful.

0 likes
12 replies
lbecket's avatar

Here are a few things you can try:

Check the URL of the API endpoint: Make sure the URL you're using to access the API is correct and includes the correct path to the API endpoint.

Check the API route: Verify that the API endpoint is defined in the route file and that it is mapped to the correct controller method.

Check the controller method: Make sure the controller method that handles the API request is functioning correctly and returning the expected data.

Enable error logging: Laravel has a built-in logging system that can help you identify the cause of the error. To enable error logging, you can add the following code to the .env file: APP_LOG=debug

Check server permissions: Make sure the server has the correct permissions to access the API files. If the server does not have the correct permissions, you may receive a 404 Not Found error.

Clear cache: Try clearing the cache by running the following command: php artisan cache:clear

Rock17's avatar

@lbecket a little more detail:

  • my public_html/.htaccess

       <IfModule mod_rewrite.c>
       <IfModule mod_negotiation.c>
             Options -MultiViews
       </IfModule>
    
       RewriteEngine On
    
       # Redirect Trailing Slashes...
       RewriteRule ^(.*)/$ / [L,R=301]
       # Handle Front Controller...
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [L]
      </IfModule>
    
  • Laravel - version 5.5.50

-php - 7.3

-part of api.php

    Route::group(['prefix' => 'v1'], function () {
    Route::post('/user-login',['as' => 'login','uses' => 
    'api\v1\UserController@user_login']);
    });
  • result for php artisan route:list

     post |          api/v1/user-login | login |  App\Http\Controllers\api\v1\UserController@user_login
    
  • I try the following options for the url:

      appdomain.com/api/v1/user-login
      appdomain.com/api/v1/login
      appdomain.com/api/v1/user_login
    

But I get error - 404

when I added file public_htm/index.php - any request to api returns 200 and that file in body

tykus's avatar

@Rock17

any request to api returns 200 and that file in body

Do you have Apache configured to handle PHP scripts?

Dragon_Worrior's avatar

Use array before prefix.

Route::group(array['prefix' => 'v1'], function () { Route::post('/user-login',['as' => 'login','uses' => 'api\v1\UserController@user_login']); });

tykus's avatar

@Dragon_Worrior what is this suggestion Route::group(array['prefix' => 'v1'], function () - is this PHP? 🤔

Dragon_Worrior's avatar

@tykus in my case its work properly Route::group(array('prefix' => 'webhook'), function() { Route::post('/md', [App\Http\Controllers\Webhook\MdController::class, 'handle']); });

tykus's avatar

@Dragon_Worrior now that is different... right?

// original solution offered
array['prefix' => 'v1'],
// latest effort...
array('prefix' => 'webhook')

In any case the array() function vs. the array literal [] syntax is not the problem here.

1 like
Rock17's avatar

@tykus - cpanel is installed on the server. I didn't configure it, it was installed by previous developers. the customer says it worked. what exactly do i need to check?

tykus's avatar
tykus
Best Answer
Level 104

@Rock17 can you check which modules are installed in Apache from within cPanel?

Rock17's avatar

@tykusI runed command

     httpd -l

This is result:

core.c
mod_co.c
http_core.c
KristianJust's avatar

In my case the opcache had cached the routes files.

So when I did php artisan route:list through the cli, it would show the correct information (as the php cli does not share opcache pool with php-fpm)

When traffic came through php-fpm, it would use old opcache.

The opcache was not cleared in my case, as I do zero downtime deployments using symlinks.

My solution was to have my deploy script restart php-fpm service.

Please or to participate in this conversation.