Search code examples
phplaravellaravel-routinglaravel-api

problem with routes API laravel in cPanel


A few days ago I uploaded my laravel project to my hosting with my test domain, in cPanel.

There I could see that my vue components were not loading, I did not see the data tables, etc.. Searching, I compiled the js and css, although the latter was not there and created a build folder in public. I uploaded the project back to hosting, and the vue components are already displayed, but the strange thing is that the API requests do not work, so the components are empty ... For example: I have a table of users, which makes a request to api/userApi, this returns me a 404, however, if I go to route:

myDomain/public/api/usersApi

Because for my project to run, I have to access public, it returns the list of users perfectly. In my .env I have defined this:

APP_URL=https://miDominio.com/public/

I have deleted cache, deleted the bootstrap folder content, deleted cache config... but it doesn't work.

In localhost all run ok

What am I doing wrong?

Update:

My htaccess:

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    php_value allow_url_fopen On
</IfModule>

Solution

  • Place a .htaccess file on your root within the project directory and add below code to it.

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,R=301]
    

    It will redirect your current request to the public folder. This is the same thing that is described in the link that I attached above.

    If it doesn't work try the below code.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.domain-name.com$
    RewriteCond %{REQUEST_URI} !folder/
    RewriteRule (.*) /folder/$1 [L]
    

    It works for me when I've same issue with the public folder