Home > Software design >  Using Laravel, can you redirect to the home page if someone visits a public storage directory?
Using Laravel, can you redirect to the home page if someone visits a public storage directory?

Time:12-21

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?

CodePudding user response:

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

Route::get('storage/{path?}', function () {
    return redirect('/');
});

CodePudding user response:

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!';
}

});

  • Related