Yii2如何访问“root”文件夹中的文件夹?

时间:2017-09-29 17:27:36

标签: yii2 yii2-advanced-app

我创建了一个包含Yii2高级模板的网站,我想访问位于“root”文件夹中的upload文件夹中的文件。 我有:

backend/web/
frontend/web/
uploads/

我已按照Yii2. Access to higher level folder

的第一个回答

所以我的.htaccess是

Options +FollowSymlinks
RewriteEngine On

# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/image/(.*)$ backend/web/image/$1 [L]

RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|image)/
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^.*$ backend/web/index.php [L]


RewriteCond %{REQUEST_URI} /(assets|css|js|img|font)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]

RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|font)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

我的common / components / Request.php文件是

<?php

  namespace common\components;

  class Request extends \yii\web\Request 
  {
     public $web;
     public $adminUrl;

     public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) .$this->adminUrl;
     }

     public function resolvePathInfo(){
        if($this->getUrl() === $this->adminUrl){
            return "";
        }else{
            return parent::resolvePathInfo();
        }
     }
  }
?>

我的fronted / config / main.php

..
'components' => [
    'request' => [
        'cookieValidationKey' => 'xxxxxxxxxxxxxxxxx',
        'csrfParam' => '_csrf-frontend',
        'class' => 'common\components\Request',
        'web'=> '/frontend/web'
    ],

现在我可以使用localhost / myapp访问我的网站(没有/ frontend / web /)

在我使用

查看上传文件夹中的图片之前
Html::img('../..'/uploads/my-image.png')

但它不再起作用了,也没有../../uploads nore with uploads /

如何访问uploads文件夹?

2 个答案:

答案 0 :(得分:1)

更改.htaccess文件。

添加上传目录 RewriteCond ,如

RewriteCond %{REQUEST_URI} !/(uploads)/ 

显示在.htaccess文件下方。

Options +FollowSymlinks
RewriteEngine On

# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/image/(.*)$ backend/web/image/$1 [L]

RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|image)/
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^.*$ backend/web/index.php [L]


RewriteCond %{REQUEST_URI} /(assets|css|js|img|font)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]

RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|font)/
RewriteCond %{REQUEST_URI} !/(uploads)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

答案 1 :(得分:0)

在vhost配置中创建一个Alias(替换公用文件夹的真实路径)并允许访问它,例如:

Alias /uploads /var/www/html/public

<Directory /var/www/html/public>
        Order allow,deny
        Allow from all
        Require all granted
</Directory>

然后你可以向/ uploads文件夹发出请求。