我知道这是一个非常流行的问题,但我还没有找到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


当前回答



请注意,当使用Docker服务Laravel项目时:你不需要做这些。只有当你的网站的根目录(或更常见的:public_html)是你的Laravel项目时才使用这个选项(当你使用Docker时不是这样)。

不!

你真的不应该把Laravel根文件夹中的server.php重命名为index.php 然后从/public目录复制。htaccess文件到你的Laravel根文件夹!!

这样,每个人都可以访问您的一些文件。例如环境)。你自己试试。你不会想要的!


DO

相反,你应该在根目录下创建一个。htaccess文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

这将无声地将所有基本uri重写到/public文件夹。甚至所有的头文件,例如HTTP授权头文件,以及所有可选的URI参数都将被默默地传递到/public文件夹。

这是所有

其他回答



请注意,当使用Docker服务Laravel项目时:你不需要做这些。只有当你的网站的根目录(或更常见的:public_html)是你的Laravel项目时才使用这个选项(当你使用Docker时不是这样)。

不!

你真的不应该把Laravel根文件夹中的server.php重命名为index.php 然后从/public目录复制。htaccess文件到你的Laravel根文件夹!!

这样,每个人都可以访问您的一些文件。例如环境)。你自己试试。你不会想要的!


DO

相反,你应该在根目录下创建一个。htaccess文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

这将无声地将所有基本uri重写到/public文件夹。甚至所有的头文件,例如HTTP授权头文件,以及所有可选的URI参数都将被默默地传递到/public文件夹。

这是所有

在根目录中创建.htaccess文件,并将如下代码放置。

<IfModule mod_rewrite.c>
 #Session timeout

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

在/public目录下创建.htaccess文件,并将代码如下所示。

<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
  </IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</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文件。

简单的方法从laravel 5 url删除公共。 你只需要从公共目录中剪切index.php和.htaccess,并将其粘贴到根目录,仅此而已 将index.php中的两行替换为

require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

注意:以上方法仅适用于初学者,因为他们在设置虚拟主机时可能会遇到问题,最好的解决方案是在本地机器上设置虚拟主机并将其指向项目的公共目录。

与Laravel 5.5相关:

在第一次安装Laravel后,我遇到了著名的“公共文件夹问题”,我想出了这个解决方案,在我个人看来,它比我在网上找到的其他解决方案更“干净”。


成就

URI中没有公共词 保护.env文件,防止好奇的人

只要使用mod_rewrite和四个简单的规则编辑.htaccess,一切都可以完成。


步骤

移动.htaccess文件到public/。Htaccess在主根目录下 编辑如下

I commented everything, so it should be clear (I hope) also to those who have never used mod_rewrite (not that I'm an expert, all the opposite). Also, to understand the rules, it must be clear that, in Laravel, if Bill connects to https://example.com, https://example.com/index.php is loaded. This file just contains the command header("refresh: 5; https://example.com/public/"), which sends the request to https://example.com/public/index.php. This second index.php is responsible to load the controller and other stuff.

# IfModule prevents the server error if the app is moved in an environment which doesn’t support mod_rewrite
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # RULES ORIGINALLY IN public/.htaccess ---
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    # --- END

    # PERSONAL RULES ---
    # All the requests on port 80 are redirected on HTTPS
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

    # When .env file is requested, server redirects to 404
    RewriteRule ^\.env$ - [R=404,L,NC]

    # If the REQUEST_URI is empty (means: http://example.com), it loads /public/index.php
    # N.B.: REQUEST_URI is *never* actually empty, it contains a slash that must be set as match as below
    # .* means: anything can go here at least 0 times (= accepts any sequence of characters, including an empty string)
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*) /public/index.php [L]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI !== css||js||images/whatever => server loads /public/index.php, which is responsible to load the app and the related controller
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^(css|js|images|media)/(.*)$ /public/index.php [L,NC]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI == css||js||images[=$1]/whatever[=$2] => server loads the resource at public/$1/$2
    # If R flag is added, the server not only loads the resource at public/$1/$2 but redirects to it
    # e.g.: bamboo.jpg resides in example.com/public/media/bamboo.jpg
    #       Client asks for example.com/media/bamboo.jpg
    #       Without R flag: the URI remains example.com/media/bamboo.jpg and loads the image
    #       With R flag: the server redirects the client to example.com/public/media/bamboo.jpg and loads the image
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
    # --- END

</IfModule>

下面的规则(原来在public/.htaccess中)可以被删除。事实上,同样的规则在最后两条规则中以更详细的方式进行了显式说明。

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

编辑:我错过了阿比纳夫·萨拉斯瓦特的解决方案,他的答案应该是公认的。只有一个简单而明确的规则,可以将所有流量重定向到公用文件夹,而无需修改任何文件。