skip to Main Content

My laravel (v5.8) project works fine in localhost , but after I uploaded it to cPanel all images and css and JS files in public html failed to load in the browser and show errors in browser console like :

"Failed to load our-approach.png:1 resource: the server responded with
a status of 404 ()"
"Failed to load resource: the main.css:1
server responded with a status of 500 ()"

While the framework itself loaded fine and show no errors.
I updated PHP to 7.4 and followed almost every youtube and StackOverflow thread and nothing worked.

index.php modified :

  require __DIR__.'/../laravel/vendor/autoload.php';

  $app = require_once __DIR__.'/../laravel/bootstrap/app.php';

AppServiceProvider.php modified

public function register()
{

  $this -> app -> bind('path.public', function()
    {
    return base_path('public_html');
    });

}

My project structure in cPanel :

|– bin
|– cache
|– laravel (project files without the public folder)
      |– app
      |– bootstrap
      |– config
      .env
      server.php
      … and the rest of the files
|– etc
|– logs
|– mail
|– public_html
      |– css
      |– js
      |– img
      .htaccess
      index.php
      …rest of files

My htaccess file in public_html :

 <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]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

2

Answers


  1. Chosen as BEST ANSWER

    Apparently the problem was in the files permission , i set it to 755 for folders and 644 for files and everything worked like charm


  2. You need changes in .htaccess file.

    Add these lines

    Options -Indexes
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    

    But please take a backup before doing changes in htaccess files

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search