Styles, font and js don't load on laravel deployed on shared hosting

58 Views Asked by At

As mentioned above, styles, font and js don't load on laravel deployed on shared hosting. How does it look like on my localhost:

localhost view

And that's same code deployed on hosting:

website view Where I take it from:

folder,

And that's how I tell me code the path:

    <link rel="stylesheet" href="{{ asset('styles/style.css') }}">
    <script src="{{ asset('js/main-page.js') }}"></script>
    <link href="https://fonts.cdnfonts.com/css/agency-fb" rel="stylesheet">

First I thought it's just public folder not loading, but then I realised that the font isn't also loaded - which is taken from https link, not from my public folder. Any ideas? My config/filesystems.php:

 'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

 'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

That's my console log

When you open it, it's just a simple 404

which I know, I just don't know why is it 404.

I tried to deploy a laravel driven code on a shared hosting. How did I do it? install newest laravel throught installatron, copyed my files, required laravel/livewire, composer update, composer dump-autoload.

I think I've tried everything. changed AppServiceProvider.php content into

public function boot()
{
    if (env('APP_ENV') !== 'local') {
        URL::forceScheme('https');
    }
}

Messed with asset to private asset, literally everything I found did not work. Maybe it's something wrong with my files structure? it looks like that:

1

There are 1 best solutions below

0
Marcin On

Solved, modyfing .htaccess worked for me. Inside of it:

# Prevent direct access to the "public" folder - redirect to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /public/
RewriteRule ^public/(.*) /$1 [R=302,L]

# Redirect Trailing Slashes If Not A Folder...
# - but look for the file in the "public" folder
#   (ensure we are not already in the "public" folder)
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]

# Rewrite "everything" to the "public" subdirectory if not already
# This ignores existing files/dirs in the document root
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteRule !^public/ public/%1

# Handle Front Controller... (as before)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Source: https://www.brixly.help/article/25-how-do-i-load-laravel-from-the-public-directory-using-htaccess