【问题标题】:laravel: how to display the assets that are inside the Public folderlaravel:如何显示公共文件夹中的资产
【发布时间】:2018-01-14 22:34:00
【问题描述】:

在服务器(共享主机)上,public 文件夹内的所有资产都不会显示。即

<img src="{{asset($painting->file)}}" >

输出这个

<img src="http://subdir.example.com/uploads/awJ4o8_miro1.jpg" >

它应该有一个 public 文件夹

http://subdir.example.com/public/uploads/awJ4o8_miro1.jpg

在我的 Public_html 文件夹中,我有这个文件夹结构

PUBLIC_HTML
├───app
├───bootstrap
├───cgi-bin
├───config
├───database
├───public
│   ├───css
│   ├───img
│   ├───js
│   ├───uploads
│   └───videos
│   .htaccess(file)
│   .index.php(file)
├───resources
├───storage
└───tests
└   .htaccess(file)

两个.htaccess文件都是这样的文件

DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ public/index.php [L]
</IfModule>

权限

所以我的问题是如何设置我的配置 (.htaccess) 以便我可以在共享托管站点上显示我的资产。

【问题讨论】:

  • 生成的url是正确的,你的public文件夹是你的文件根目录,不应该包含在url中。您是否使用/public 访问您的网站?
  • 嗨@Jeredov。不,我没有使用/publi/ 访问我的网页,但如果我更改 url 并将 public 添加到其中,一切正常http://miro.mongexweb.ca/public/uploads/NUsZlC__MG_5032.jpg

标签: laravel .htaccess laravel-5.1 shared-hosting laravel-storage


【解决方案1】:

您可以在最后一条规则上方插入此规则以将资产重定向到public/

DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteRule ^uploads(/|$) /public%{REQUEST_URI} [L,NC,R=301,NE]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ public/index.php [L]
</IfModule>

【讨论】:

    【解决方案2】:

    您可以在资产 url 前加上 public 为:

    {{ asset('public/' . $painting->file) }}
    

    【讨论】:

      猜你喜欢
      • 2020-01-27
      • 2020-02-13
      • 2021-03-18
      • 2018-02-13
      • 1970-01-01
      • 2017-04-02
      • 2015-08-08
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多