简体   繁体   中英

How do I remove /storage from the URL for public images in my Laravel project?

What I am trying to achieve is to remove the /storage from the URL, so that in the end it is www.example.com/images/x.jpg and not the default www.example.com/storage/x.jpg .

I have tried removing /storage from the url in config/filesystems.php like this:

// Original

'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL') . '/storage',
        'visibility' => 'public',
 ],

// Modified

'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL'), // <-- changed
        'visibility' => 'public',
 ],

but it is not working. I think the issue is that without a prefix it will be regarded as file inside a public folder.

Is it possible to achieve what i am trying to achieve?

The most straightforward way to accomplish this is to add a new disk. This way you can apply the new pattern to your images without affecting existing files and urls, along with a number of other benefits.

Step 1

Add your disk to config/filesystems.php :

'images' => [
        'driver' => 'local',
        'root' => storage_path('app/public/images'),
        'url' => env('APP_URL') . '/images',
        'visibility' => 'public',
 ],

Here is an example of how to save file uploads to your new disk from a controller:

// storeAs: path, filename, disk
$request->file('image')->storeAs('/', 'x.jpg', 'images')

And this is how you generate links to the image that look like http://example.com/images/x.jpg :

Storage::disk('images')->url('x.jpg')

Step 2

Here are three different options for serving files from the new path ( you only need to pick one ):

Option 1

Create a symlink in your public directory.

ln -s /var/www/example.com/storage/app/public/images /var/www/example.com/public/images

This is the same method Laravel uses for the default public disk (the /storage URLs).

As of Laravel 7, you can modify config/filesystems.php to manage additional symlinks:

    /*
    |--------------------------------------------------------------------------
    | Symbolic Links
    |--------------------------------------------------------------------------
    |
    | Here you may configure the symbolic links that will be created when the
    | `storage:link` Artisan command is executed. The array keys should be
    | the locations of the links and the values should be their targets.
    |
    */
    'links' => [
        public_path('storage') => storage_path('app/public'),
        public_path('images')  => storage_path('app/public/images'),
    ],

Option 2

Create a route in your Laravel application to serve images:

Route::get('images/{file}', function ($file) {
    return Storage::disk('images')->response($file);
    // or to trigger downloads:
    // return Storage::disk('images')->download($file);
});

The disadvantage to this option is that it serves each image using a PHP process, instead of being handled by the webserver like options 1 & 3.

Option 3

Create a rewrite rule in your webserver.

In nginx, it might look like this:

location /images/ {
    root /var/www/example.com/storage/app/public/;
}

In Apache, you might use an alias :

Alias "/images" "/var/www/example.com/storage/app/public/images"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM