Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All css/img files route to the site's homepage

I have pulled a Laravel application from a remote server and trying to set up a local version.

I copied all the files and database, and managed to server up the local application. All the routes seems to work, except for one big problem - all the assets from the public folder route to the site's homepage!

For, example, I have an image included like so:

<img src="{{ asset('img/main-logo.png') }}" />

On the production server, this displays the image, with the source URL https://www.example.com/img/main-logo.png and this works correctly.

On my local server, the image source turns out to be http://localhost:8005/img/main-logo.png, which should be correct, but it doesn't show the image. When I try to open this URL in the browser, instead of showing the image it opens the site homepage!

I have the server.php file in the root folder, index.php in public, and the following .htaccess in my public folder (copied from the production server):

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

    # Force compression for mangled headers.
    # https://developer.yahoo.com/blogs/ydn/pushing-beyond-gzipping-25601.html

    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    <IfModule mod_headers.c>
     <filesMatch "\.(jpg|jpeg|png|gif|webp|js|ico)$">
        Header set Cache-Control "max-age=2628000, public"
     </filesMatch>
    </IfModule>

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Map certain file types to the specified encoding type in order to
    # make Apache serve them with the appropriate `Content-Encoding` HTTP
    # response header (this will NOT make Apache compress them!).

    # If the following file types wouldn't be served without the appropriate
    # `Content-Enable` HTTP response header, client applications (e.g.:
    # browsers) wouldn't know that they first need to uncompress the response,
    # and thus, wouldn't be able to understand the content.

    # http://httpd.apache.org/docs/current/mod/mod_mime.html#addencoding

    <IfModule mod_mime.c>
        AddEncoding gzip              svgz
    </IfModule>

    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE "application/atom+xml" \
                                      "application/javascript" \
                                      "application/json" \
                                      "application/ld+json" \
                                      "application/manifest+json" \
                                      "application/rdf+xml" \
                                      "application/rss+xml" \
                                      "application/schema+json" \
                                      "application/vnd.geo+json" \
                                      "application/vnd.ms-fontobject" \
                                      "application/x-font-ttf" \
                                      "application/x-web-app-manifest+json" \
                                      "application/xhtml+xml" \
                                      "application/xml" \
                                      "font/opentype" \
                                      "image/svg+xml" \
                                      "image/x-icon" \
                                      "text/cache-manifest" \
                                      "text/css" \
                                      "text/html" \
                                      "text/javascript" \
                                      "text/plain" \
                                      "text/vtt" \
                                      "text/x-component" \
                                      "text/xml"
    </IfModule>

    RewriteEngine On

    RewriteRule ^robots\.txt$ robots.php

    # rewrite non www to www
    RewriteCond %{HTTP_HOST} ^localhost:8005 [NC]
    RewriteRule ^(.*)$ http://localhost:8005/$1 [L,R=301,NC]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # redir for last / in url
    RewriteCond %{REQUEST_METHOD} =GET
    RewriteRule ^(.*)\/(\?.*)?$ /$1$2 [NC,L,R=301]

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

</IfModule>

So it's the same .htaccess, same routes, yet the assets that load on the production server don't load locally. Anyone know why that may be and how to fix it?

I have tried adding 'public/' to the asset URLs, tried absolute/relative paths instead too, nothing works.

EDIT: in the meanwhile I have tried editing .htaccess in various ways, and even removing it completely, and nothing changed in the way the website handles the routing for public assets.

EDIT #2: while http://localhost:8005/img/main-logo.png loads the site's homepage, http://localhost:8005/public/img/main-logo.png gives a 404 error

EDIT #3: when I load http://localhost:8005/img/main-logo.png it shows this under 'routing':

enter image description here

like image 828
sveti petar Avatar asked Nov 07 '22 23:11

sveti petar


1 Answers

i had a same problem and i fixed it. try this. I changed /vendor/Illuminate/Foundation/helpers.php asset() function as follows:

function asset($path, $secure = null)
{
    return app('url')->asset("public/".$path, $secure);
}

Hope it will work

like image 195
cijagani Avatar answered Nov 11 '22 08:11

cijagani