Search code examples
laravel.htaccessweb-hostingsymlink

Cannot access symlinked file in shared hosting in Laravel


I use hostinger and I have two folders in my parent hosting directory

  1. my-laravel-app-folder
  2. public_html

in public/index.php I changed

  1. require __DIR__.'/../vendor/autoload.php'; to require __DIR__.'/../my-laravel-app-folder/vendor/autoload.php';
  2. $app = require_once __DIR__.'/../bootstrap/app.php'; to $app = require_once __DIR__.'/../my-laravel-app-folder/bootstrap/app.php';

in public/.htaccess file I changed

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

    RewriteEngine On
    Options +FollowSymLinks


    # 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
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^ index.php [L]
</IfModule>

From parent / directory ,I have already run

ln -s my-laravel-app-folder/storage/app/public public_html/storage

But I still cannot access to linked storage files and got 404 error.


Solution

  • Finally I found the solution, it's when creating a symlink ,I have to include absolute path for folders instead of current path .For example,

    • For my-laravel-app-folder/storage/app/public replaced with /home/u440262284/domains/mysite.com/my-laravel-app-folder/storage/app/public instead and
    • For also public_html/storage replaced with /home/u440262284/domains/mysite.com/public_html/storage

    It worked !!!