在将文档根目录设置为/public后,无法访问公共资源


Can't access assets on public after setting document root to /public

我最近刚开始在一个新项目中使用Laravel,我遇到了一个问题。

所以,我想我已经为一个新项目准备好了一切,并开始工作了。在我的本地机器(Windows,运行WAMP)上一切都很好,我已经在个人VPS (Ubuntu,运行NGINX)上设置了它,这也很好。现在,我的雇主正在使用共享主机,遗憾的是访问非常有限。我有ftp访问权限,他可以访问cPanel上的一些东西。我已经尝试部署到服务器直接从我的本地项目与PhpStorm。它看起来不错,唯一的问题是它指向一个文件列表,但当访问serverlink/public时,它看起来不错。我已经要求我的雇主将文档根目录更改为/public,然后一切都开始走下坡路,因为我现在无法访问我在/public上的资产(css/images)。生成的链接是他们应该的,例如:serverlink/css/mycss.css,但文件返回404,这是相当奇怪的。

这是我的htaccess:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

在我的视图中,我使用blade引用资产,像这样:

<link href="{{ asset('css/button.css') }}" rel="stylesheet" type="text/css" >

当我试图在浏览器中直接访问文件时,我得到的是

"对不起,找不到您要找的页面。"1/1 RouteCollection.php中的NotFoundHttpException行145:"

有谁能帮我吗?快疯了,尤其是在我的副总裁身上效果很好!我是否需要因为托管限制而放弃Laravel 5,或者我是否可以绕过它们?

欢呼,蒂亚戈c .

添加一行index.php

 $app->bind('path.public', function() {
    return __DIR__;
});

将公共目录改为主目录

原来是根目录上的.htaccess需要身份验证的问题。不知何故,我猜当根文件夹被设置为公共,其他。htaccess阻止访问文件(因为授权没有被要求)。后来我把它编辑了出来,并把我的。htaccess (inside public)改成了:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
RewriteBase /public
    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</IfModule>

这也消除了需要"index.php/"或类似的在我的路由。

干杯!