skip to Main Content

I’ve made a website using Laravel, but I’d like to make it redirect if a user tries to visit a public storage directory (not an image/file). Currently it displays a 403 error).

For example, an image might be hosted at "mywebsite.com/storage/uploads/Image1.jpg", if a user removes the "Image1.jpg" part and tries to visit "mywebsite.com/storage/uploads/" or "mywebsite.com/storage/", a 403 error is shown.

I would prefer if the user was redirected to the home page in this case. How can I accomplish this?

2

Answers


  1. You can try adding this in the web/routes.php

    Route::get('storage/{path?}', function () {
        return redirect('/');
    });
    
    Login or Signup to reply.
  2. use the code below

    Route::get('files/{pathToFile}', function($pathToFile) {
    
    if (auth()->user()->hasAccessToFile($pathToFile)) {
        return response()->file($pathToFile);
    } else {
        return 'Nope, sorry bro, access denied!';
    }
    

    });

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search