【问题标题】:Yii2 .htaccess prevent to access images in backendYii2 .htaccess 阻止访问后端的图像
【发布时间】:2016-07-30 07:45:41
【问题描述】:

我的 yii2 根目录中有 .htaccess 文件来隐藏前端/web,我正在 yii2-app/uploads 中上传图片。 问题是由于RewriteRule ^(.*)$ frontend/web/$1 [L] 这一行,我无法访问后端中的图像,如果我删除此行,则图像可以访问,但前端/web 出现在 url 中,我该如何解决这个问题?如何创建访问图像的特殊规则?

在网格视图中:

[
 '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:

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]

目录结构:

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

【问题讨论】:

标签: php .htaccess yii2 yii2-advanced-app


【解决方案1】:

我在RewriteRule ^(.*)$ frontend/web/$1 [L] 之前添加了这条规则并为我工作。

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

【讨论】:

  • 同样在高级配置中,您可以通过存储替换上传以使存储目录被发现
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多