2016-05-03 57 views
3

我在laravel 5.2路由到公共文件夹的图像不点火

Route::get('images/{filename}', function ($filename) 
{ 
    if($filename) 
    { 
     return redirect('/home'); 
    }  
}); 

如果一个人进入以下网址如下路线:www.mydomain.com/images/image_abc123.jpg,我希望它将被重定向到www.mydomain.com/home。但是路线不是触发的,而是去显示图像。

有没有建议吗?谢谢!

编辑:这里是我的.htaccess laravel 5.2/public目录

<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> 
+0

您是否尝试过定义中间件? –

+0

是的,先生,我有。它也没有被触发。就是这样或者我做错了。 –

+0

你是怎么做到的?你能告诉我吗? –

回答

0

好吧!公共文件夹被命名为公共的原因! 解决这个问题,你必须把这些图像的存储文件夹(这就是存储文件夹是什么,如果你有没有想过)

Storage/app 

所以他们不公开文件夹中存在了,人们不能,除非 达到这些目标你这样做

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']); 

}); 

要达到=防止某些人或所有人从accesing文件什么 什么答案呢=从公职阻止文件,除非你使用的方法 我希望一切都清楚了服务主题

1

这可能是因为你的网络服务器下的默认。通常的默认设置通常是这样的。

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_files指令。这是告诉网络服务器:

  1. 如果请求是一个现存的文件,打开它。
  2. 如果请求是一个现存目录,请对其进行索引。
  3. 否则,将目录作为参数传递给/index.php。

因此,第一遍,“该文件是否存在?”,它说“是”,并绕过PHP 100%。你会想改变它。

+0

谢谢乔希。你的回答是有道理的。我正在使用laravel 5.2的默认.htaccess,我该如何修改它? –

+0

查看源代码。您需要删除'RewriteCond%{REQUEST_FILENAME}!-f'这一行。请注意,这将打破CSS和JS的所有请求。如果你需要更多关于编辑.htaccess文件的建议,你应该提出另一个SO问题。 https://github.com/laravel/quickstart-basic/blob/master/public/.htaccess – Josh

相关问题