Search code examples
laravelapachecpanel

Image not loading on shared hosting running cpanel and apache


I deploy me laravel app to shared hosting service.

Now here is how deployed it;

The laravel app is in a separate directory, now being a shared hosting service, I copied the content of laravel/public to /public_html and edited public_html/index.php to link to laravel folder.

I am able to access this site and html is loading fine but I am unable to load image url. Image load with 404. Mind you, I have symlink laravel/storage/app/public/ to public_html/storage and I can all the images there.

Any reason why I cannot load image.

I have also tried enabling/disabling hotlinking on cpanel but to no avail.

Edited

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

Solution

  • Maybe the symlink itself has the wrong path? If you have created it via the instruction in the Laravel Docs: https://laravel.com/docs/9.x/filesystem#the-local-driver

    ...then the symlink was not created relative, which means that if the absolute path to the storage has changed, it won't work anymore.

    The solution therefore could be to create a relative symlink with the symfony/filesystem package:

    php artisan storage:link --relative
    

    As provided in THIS answer.

    This has solved my issues with accessing the images.