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

Yii2 How to access a folder in "root" folder?

我用 Yii2 高级模板制作了一个网站,我想访问位于 "root" 文件夹的上传文件夹中的文件。 我有:

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/ [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/ [L]
RewriteRule ^backend/image/(.*)$ backend/web/image/ [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/ [L]
RewriteRule ^css/(.*)$ frontend/web/css/ [L]
RewriteRule ^js/(.*)$ frontend/web/js/ [L]
RewriteRule ^img/(.*)$ frontend/web/img/ [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 还是 uploads/

如何访问上传文件夹?

在虚拟主机配置中创建一个别名(替换 public 文件夹的真实路径)并允许访问它,例如:

Alias /uploads /var/www/html/public

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

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

.htaccess 文件中的更改。

添加 uploads 目录 RewriteCond 喜欢

RewriteCond %{REQUEST_URI} !/(uploads)/ 

.htaccess 文件下方显示。

Options +FollowSymlinks
RewriteEngine On

# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/ [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/ [L]
RewriteRule ^backend/image/(.*)$ backend/web/image/ [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/ [L]
RewriteRule ^css/(.*)$ frontend/web/css/ [L]
RewriteRule ^js/(.*)$ frontend/web/js/ [L]
RewriteRule ^img/(.*)$ frontend/web/img/ [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