Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

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. 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? 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:

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

Upvotes: 1

Views: 1534

Answers (1)

Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

I have added this rule before RewriteRule ^(.*)$ frontend/web/$1 [L] and working for me.

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

Upvotes: 2

Related Questions