我知道这是一个非常流行的问题,但我还没有找到Laravel 5的有效解决方案。很长一段时间以来,我一直试图从Codeigniter迁移,但这个复杂的安装过程一直让我望而却步。

我不想运行一个虚拟机,这只是看起来尴尬时切换项目。

我不想将我的文档根文件夹设置为公共文件夹,这在项目之间切换时也很尴尬。

我已经尝试了.htaccess mod_rewrite方法

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

这只是给了我一个Laravel NotFoundHttpException在compiled.php行7610。

当我前段时间尝试L4时,我使用了将公共文件夹的内容移动到根目录的方法。L5的结构完全不同,遵循相同的步骤完全破坏了Laravel(服务器只会返回一个空白页面)。

在开发环境中是否有一种合适的方法来删除“public”:

与L5一起工作 允许我轻松地在项目之间切换(我通常同时工作2或3个项目)。

谢谢

**我使用MAMP和PHP 5.6.2


当前回答

在Laravel 5.5中,在根目录中创建。htess文件,并放置以下代码

<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>

其他回答

@rimon。ekjon说:

将Laravel根文件夹中的server.php重命名为index.php,并将/public目录中的.htaccess文件复制到Laravel根文件夹中。——就是这样!!:)

这对我很管用。 但是/public目录中的所有资源文件都找不到,请求url也不工作,因为我使用了asset() helper。

我修改了/Illuminate/Foundation/helpers.php/asset()函数如下:

function asset($path, $secure = null)
{
    return app('url')->asset("public/".$path, $secure);
}

现在一切正常了:)

谢谢你@rimon。艾克乔恩和你们所有人。

2020年作者更新

不建议使用此答案。 相反,建议处理.htaccess文件。

只需在根目录下创建.htaccess文件,并将这些行添加到其中

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

就是这样!

上面的代码适用于根public_html文件夹。已经说过,你的核心laravel文件应该在public_html文件夹中,如果你的目录看起来像public_html/laravelapp/public,如果你把上面的代码放在laravelapp中,它将不起作用。因此,必须将所有核心文件复制到public_html中,并将.htaccess文件放在那里。

如果你想将代码保存在子目录中,那么你可以创建一个子域,那么这段代码也可以用于该子目录。

问题是如果你输入/public,它仍然会在url中可用,因此我创建了一个修复,应该放在public/index.php

  $uri = urldecode(
     parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  );

  if(stristr($uri, '/public/') == TRUE) {

    if(file_exists(__DIR__.'/public'.$uri)){

    }else{

      $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
      $actual_link = str_replace('public/', '',$actual_link);
      header("HTTP/1.0 404 Not Found");
      header("Location: ".$actual_link."");
      exit();
      return false;
   }}

这段和平代码将从url中删除public,并给出404,然后重定向到没有public的url

您可以使用各种方法从url中删除公共关键字。

1)如果你使用专用主机,你有根访问权限,那么你可以使用虚拟主机从url中删除公共关键字。你应该给DocumentRoot路径加上public。这会启动index from public directory并从url中移除它。

<VirtualHost *:80>
    ServerAdmin info@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/{yoursourcedirectory}/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2)如果你没有你的主机的根访问权限,那么你应该在你的根目录下生成一个新的。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>

你可以在这里得到更多的参考资料。

在Laravel设置中总是有一个公共文件夹的原因,所有与公共相关的东西都应该出现在公共文件夹中,

不要将你的ip地址/域名指向Laravel的根文件夹,而是指向公共文件夹。将服务器Ip指向根文件夹是不安全的。因为除非你在.htaccess中写了限制,否则一个文件可以很容易地访问其他文件。

只要在.htaccess文件中写入重写条件,并安装重写模块并启用重写模块,就可以解决路由中添加public的问题。