Laravel 路由到未启动的公用图像文件夹

Laravel 路由到未启动的公用图像文件夹,laravel,routes,Laravel,Routes,我在拉雷维尔5.2有以下路线 Route::get('images/{filename}', function ($filename) { if($filename) { return redirect('/home'); } }); 如果某人输入以下url:www.mydomain.com/images/image_abc123.jpg,我希望它会被重定向到www.mydomain.com/home。但是路由没有触发,而是显示图像 有什么建议吗

我在拉雷维尔5.2有以下路线

Route::get('images/{filename}', function ($filename)
{
    if($filename)
    {
        return redirect('/home');
    }    
});
如果某人输入以下url:www.mydomain.com/images/image_abc123.jpg,我希望它会被重定向到www.mydomain.com/home。但是路由没有触发,而是显示图像

有什么建议吗?谢谢大家!

编辑:这里是my.htaccess/public目录下laravel 5.2的默认值

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

选项-多视图
重新启动发动机
#如果不是文件夹,则重定向尾部斜杠。。。
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)/$/$1[L,R=301]
#处理前控制器。。。
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]
#句柄授权头
RewriteCond%{HTTP:Authorization}。
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]

这可能是因为您的Web服务器。常见的默认值通常如下所示

server {
        listen 80;
        listen [::]:80;
        listen 443 ssl;
        listen [::]:443 ssl;

        server_name cards.foo *.cards.foo;
        root /var/www/cards/public;

        index index.php;
        include php.conf;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }
}
请注意
try\u files
指令。这告诉Web服务器:

  • 如果请求是现有文件,请打开它
  • 如果请求是一个现存目录,则为其编制索引
  • 否则,将目录作为参数传递给/index.php

  • 因此,在第一个过程中,“该文件存在吗?”,它说“是”,并100%绕过PHP。你会想改变的。

    好吧!公用文件夹被命名为公用是有原因的! 要解决这个问题,您必须将这些图像放在存储文件夹中(如果您想知道存储文件夹是什么的话,这就是存储文件夹)

    所以它们不再存在于公共文件夹中,除非 你这么做

    Route::get('images/{filename}', function ($filename)
    {
         // get the image named $slug from storage and display it
        // Something like (not sure)
    
           $image = File::get('images/' . $filename . '.jpg');
    
            return response()->make($image, 200, ['content-type' => 'image/jpg']);
    
    });
    
    您想要实现的目标=阻止部分或所有人访问文件 答案的作用=防止文件公开,除非您使用方法提供主题
    我希望一切都清楚

    谢谢你,乔希。你的回答有道理。我正在使用laravel 5.2的默认.htaccess,如何修改它?请查看源代码。您需要删除行
    RewriteCond%{REQUEST\u FILENAME}-f
    。请注意,这也将中断对CSS和JS的所有请求。如果您需要更多关于编辑.htaccess文件的建议,您应该提出另一个问题。你试过了吗?是的,先生,我试过了。它也不会被触发。否则我就做错了。你是怎么做的?你能给我看看吗?
    Route::get('images/{filename}', function ($filename)
    {
         // get the image named $slug from storage and display it
        // Something like (not sure)
    
           $image = File::get('images/' . $filename . '.jpg');
    
            return response()->make($image, 200, ['content-type' => 'image/jpg']);
    
    });