Search code examples
phplaravelapacheubuntu-20.04laravel-9

404 on GET request Laravel 9


I'm working on a project with the new version of Laravel. My other projects are all previous versions. I tried every artisan clean method to make everything clear. I checked the server requirements, which are precisely the same as how I run my other Laravel 8 projects. But when I request by Postman my API, it returns a 404 page instead of the actual action.

Route

GET|HEAD   api/token ... generated::*** › UserController@tokenRequest

api.php

Route::get('token', [UserController::class, 'tokenRequest']);

I return a simple response with a token. If there is a PHP error, I'll see it and not return with a 404, which is weird. I had the same problem with my other projects, but it was just a mod_rewrite issue, but I all checked those options. Each is set correctly in the .conf. I use apache on my Ubuntu 20.04 server, like my other projects. The only difference is the Laravel version.

URL that I use:

https://example.com/directory/api/token

Public directory URL is working perfectly.

https://example.com/directory/public

Custom domain .conf

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@domain1.com
DocumentRoot /var/www/html

<Directory /var/www/html>
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>

ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com.log combined

Solution

  • I usually setup vhost for each project and this is how it looks like for me:

    <VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@domain1.com
    DocumentRoot /var/www/html/example.com/public
    
    <Directory "/var/www/html/example.com/public">
        AllowOverride All
        ReWriteEngine On
        Options Indexes MultiViews FollowSymLinks
        Order allow,deny
        allow from all
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com.log combined
    

    and this worked for all my laravel apps