问题
I have made a website with Yii2 advanced template and I want to access files in the upload folder located in the "root" folder. I have :
backend/web/
frontend/web/
uploads/
I have followed the 1st answer of Yii2. Access to higher level folder
So my .htaccess is
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
My common/components/Request.php file is
<?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();
}
}
}
?>
My fronted/config/main.php
..
'components' => [
'request' => [
'cookieValidationKey' => 'xxxxxxxxxxxxxxxxx',
'csrfParam' => '_csrf-frontend',
'class' => 'common\components\Request',
'web'=> '/frontend/web'
],
Now I can access to my site with localhost/myapp (without /frontend/web/)
Before I could see the image in my upload folder with
Html::img('../..'/uploads/my-image.png')
But it does not work anymore, neither with ../../uploads nore with uploads/
How can I access the uploads folder?
回答1:
Change in .htaccess
file.
Add uploads directory RewriteCond like
RewriteCond %{REQUEST_URI} !/(uploads)/
show below .htaccess
file.
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
回答2:
In vhost configuration create an Alias (replace real path of public folder) and allow to access to it, such as:
Alias /uploads /var/www/html/public
<Directory /var/www/html/public>
Order allow,deny
Allow from all
Require all granted
</Directory>
Then you can make requests to /uploads folder.
来源:https://stackoverflow.com/questions/46494061/yii2-how-to-access-a-folder-in-root-folder