简体   繁体   English

Yii2 .htaccess阻止访问后端中的图像

[英]Yii2 .htaccess prevent to access images in backend

I have .htaccess file in my yii2 root directory to hide frontend/web and I'm uploading images in yii2-app/uploads. 我的yii2根目录中有.htaccess文件,用于隐藏前端/网络,并且正在yii2-app / uploads中上传图像。 Issue is I can't access images in backend due to this line RewriteRule ^(.*)$ frontend/web/$1 [L] , if I delete this line then images are accessible but frontend/web appears in url, How I can solve this? 问题是由于此行RewriteRule ^(.*)$ frontend/web/$1 [L] ,我无法在后端访问图像,如果我删除此行,则图像可以访问,但前端/ web显示在url中,我该如何解决这个? How I can create a special rule for access images? 如何为访问图像创建特殊规则?

In grid view: 在网格视图中:

[
 'label' => 'Image', 
 'attribute' => 'banner', 
 'format' => 'raw',   
 'value' => function ($data) {
      return Html::img(Yii::$app->request->baseUrl.'../../../uploads/'.$data->banner, ['alt'=>$data->title,'width'=>'20','height'=>'30']); 
   }
],   

.htaccess: 的.htaccess:

Options -Indexes

<IfModule mod_rewrite.c> 
  RewriteEngine on
  #RewriteCond %{REQUEST_URI} !^public
  RewriteRule ^(.*)$ frontend/web/$1 [L] 
</IfModule>

# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>

# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]

Directory structure: 目录结构:

yii2-app
   --backend
   --frontend
   --uploads

I have added this rule before RewriteRule ^(.*)$ frontend/web/$1 [L] and working for me. 我在RewriteRule ^(.*)$ frontend/web/$1 [L]之前为我添加了此规则。

RewriteCond %{REQUEST_URI} /(uploads)
RewriteRule ^uploads/(.*)$ uploads/$1 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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