Laravel: Images Work Locally But Show 404 on Live Server Website?

After pushing your code to your live server, the website doesn't show the public images you uploaded? This is a typical problem often asked on forums. These are the potential reasons/fixes.

Of course, it depends on how you load the image. The typical code is this:

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

Or, if you use a Spatie Media Library package:

<img src="{{ $user->getFirstMediaUrl('avatars') }}" />

First, what you need to do is to right-click that failed image and see its full URL. From that, you may realize your problem. Does that image contain an incorrect domain? Some subfolders missing in the path?

Then, check these three things:

  1. Check your .env file and APP_URL value: it should be https://[yourwebsite.com] - some people forget to change it from the default localhost or provide incorrect URL with an extra folder/subdomain
  2. If you store images in storage/app/public, check if you have run php artisan storage:link successfully (so, no errors happened) to create the symlink.
  3. Check if your web-server root folder is successfully pointed to the /public folder of the Laravel project.

In my experience, these cover all potential cases of this problem.

No comments yet…

avatar
You can use Markdown

Recent New Courses