Harsh Patel
Harsh Patel

Reputation: 1324

After Deployment, Images not showing in Laravel

I created Common Helper for Image Upload & Common Trait Function for get Images Full URL from Storage & I tested it in Laravel's default inbuilt server it's 1005 perfectly works.

but after Deployment I can't Display Images. it's show 404 error to me

I shared my Common function of Get Full URL path of Image from storage.

app/Traits>HasProfilePhoto.php file

public function getProfilePhotoUrlAttribute()
{
    return $this->profile_photo_path
                ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
                : \URL::to('/').'/adminLTE/dist/img/user-img.png';
}

above function will returns Image's Full URL Path Like."test.com/storage/users/user.png"

using Laravel's Default Inbuilt server it returns Full URL like this. http://localhost:8000/storage/users/user.png & using that path image display perfectly

but after the deployment in local Windows using XAMPP Server, it returns Full URL like this. http://localhost/ProjectFolderName/storage/users/user.png & it's show 404 Not Found

NOTE:- If I use FULL Url like this:- http://localhost/ProjectFolderName/public/storage/users/user.png then image displayed

Please Help me what's my mistake there?

* **How I deployed my Laravel Project see below:- **

*

root/server.php file renamed with index.php

& root/public/.htaccess file copied at root directory

Upvotes: 1

Views: 3081

Answers (2)

Mirko Dzudzar
Mirko Dzudzar

Reputation: 61

Maybe you just need to set your projects URL inside .env to start with http://

APP_URL=http://your-project-url

Without this, images will not have proper URL but when you copy image path and open it inside new tab, image will be available anyway.

Upvotes: 1

Amit Senjaliya
Amit Senjaliya

Reputation: 2945

Rename the server.php file to index.php is not good practice for security reasons as well. So if you need to hide the public path from the URL then create a .htaccess at root level:

<ifmodule mod_rewrite.c>

    <ifmodule mod_negotiation.c>
        Options -MultiViews
    </ifmodule>
 
    RewriteEngine On
 
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</ifmodule>

Updated by Harsh Patel:- 8th September 2021

Actually Problem is about defining Virtual Host in Server... I was defined DocumentRoot path & Directory path as like "C:/xampp/htdocs/projectFolderName/ this.

I was identified my mistake in deployment from here medium's Blog

but as per Laravel Documentation , we need to direct all requests to public/index.php in other words Laravel's Public Folder is an Our Server's Root (public_html) folder that's why I need to define DocumentRoot & Directory up to public folder like this C:/xampp/htdocs/projectFolderName/public

  • We need to define our Laravel Web in Apache Server's Virtual Host Config.

for XAMPP Server

step 1: open C:\xampp\apache\conf\extra path location in XAMPP Server

step 2: Open httpd-vhosts.conf File

step 3: define virtual host rule as like below

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot "C:/xampp/htdocs/projectFolderName/public"
  <Directory "C:/xampp/htdocs/projectFolderName/public">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

IF you faced any problem to define Virtual Host in XAMPP then you can see Full detailed answer about How to define Virtual Host in XAMPP

& If you Use WAMPP Server then you can see Full detailed answers about How to define Virtual Host in WAMPP

Upvotes: 1

Related Questions