Search code examples
laravel.htaccesslaravel-5.1shared-hostinglaravel-storage

laravel: how to display the assets that are inside the Public folder


On the server (shared-hosting) all the assets that are inside the public folder are not displayed. i.e.

<img src="{{asset($painting->file)}}" >

output this

<img src="http://subdir.example.com/uploads/awJ4o8_miro1.jpg" >

it supposed to have a public folder

http://subdir.example.com/public/uploads/awJ4o8_miro1.jpg

On my Public_html folder I have this folder structure

PUBLIC_HTML
├───app
├───bootstrap
├───cgi-bin
├───config
├───database
├───public
│   ├───css
│   ├───img
│   ├───js
│   ├───uploads
│   └───videos
│   .htaccess(file)
│   .index.php(file)
├───resources
├───storage
└───tests
└   .htaccess(file)

both of the .htaccess file is like this file

DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

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

permissions

enter image description here

So my questions is how can I set my configuration (.htaccess) so that I can display my assets on the shared-hosting site.


Solution

  • You can insert this rule just above last rule to redirect assets to public/:

    DirectoryIndex public/index.php
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        RewriteRule ^uploads(/|$) /public%{REQUEST_URI} [L,NC,R=301,NE]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ public/index.php [L]
    </IfModule>