【问题标题】:Image not loading from custome-directory of public directory at Laravel 6图像未从 Laravel 6 的公共目录的自定义目录加载
【发布时间】:2020-06-30 12:05:36
【问题描述】:

我将所有图像存储在 laravel 项目根文件夹的自定义目录中。


自定义目录意味着我在根目录中创建了像“upload_files”这样的新文件夹。

每当我从这个目录 url 调用任何文件时,就像“http://domainName.com/upload_files/folderName/fileName.jpg
但它是在网页视图中加载的。


注意:图像是从公共目录加载的。
下面是我的 .htaccess 文件代码
注意:我在根目录创建了新的 .htaccess 文件,而不是公共文件夹 .htaccess 两个文件都存在于项目中
<IfModule mod_rewrite.c>
   <IfModule mod_negotiation.c>
      Options -MultiViews
    </IfModule>

     RewriteEngine On

     RewriteCond %{REQUEST_FILENAME} -d [OR]
     RewriteCond %{REQUEST_FILENAME} -f
     RewriteRule ^ ^$1 [N]

   RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
   RewriteRule ^(.*)$ public/$1



   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ server.php

   </IfModule>

   # Deny index view Options -Indexes

   # Deny view a specific file <Files .env>    Order allow,deny    Deny from all </Files>

【问题讨论】:

    标签: laravel image


    【解决方案1】:

    在这些行之前

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1
    

    尝试添加这个

    RewriteCond $1 !^(upload_files)
    

    这将允许访问 upload_files

    <IfModule mod_rewrite.c>
       <IfModule mod_negotiation.c>
          Options -MultiViews
        </IfModule>
    
         RewriteEngine On
    
         RewriteCond %{REQUEST_FILENAME} -d [OR]
         RewriteCond %{REQUEST_FILENAME} -f
         RewriteRule ^ ^$1 [N]
    
         RewriteCond $1 !^(upload_files)
    
         RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
         RewriteRule ^(.*)$ public/$1
    
    
    
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule ^ server.php
    
       </IfModule>
    
       # Deny index view Options -Indexes
    
       # Deny view a specific file <Files .env>    Order allow,deny    Deny from all </Files>
    

    这应该可以解决问题

    RewriteRule ^(.*)$ public/$1
    

    表示您的所有请求都将发送到公共文件夹。所以这里我们排除了upload_files的请求

    RewriteCond $1 !^(upload_files)
    

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2017-09-26
      • 2015-02-15
      • 2016-07-25
      • 1970-01-01
      • 2019-03-28
      • 2011-02-24
      • 2014-06-14
      • 1970-01-01
      相关资源
      最近更新 更多