Yii2 .htaccess 防止在后端访问图像
Yii2 .htaccess prevent to access images in backend
我的 yii2 根目录中有 .htaccess 文件来隐藏 frontend/web,我正在 yii2-app/uploads 中上传图片。
问题是由于这一行 RewriteRule ^(.*)$ frontend/web/ [L]
,我无法在后端访问图像,如果我删除这一行,则可以访问图像,但 frontend/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/ [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
我在 RewriteRule ^(.*)$ frontend/web/ [L]
之前添加了这条规则并为我工作。
RewriteCond %{REQUEST_URI} /(uploads)
RewriteRule ^uploads/(.*)$ uploads/ [L]
我的 yii2 根目录中有 .htaccess 文件来隐藏 frontend/web,我正在 yii2-app/uploads 中上传图片。
问题是由于这一行 RewriteRule ^(.*)$ frontend/web/ [L]
,我无法在后端访问图像,如果我删除这一行,则可以访问图像,但 frontend/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/ [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
我在 RewriteRule ^(.*)$ frontend/web/ [L]
之前添加了这条规则并为我工作。
RewriteCond %{REQUEST_URI} /(uploads)
RewriteRule ^uploads/(.*)$ uploads/ [L]