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


当前回答

只需在根目录下创建.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文件放在那里。

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

其他回答

我以前读过一些文章,它工作得很好,但真的不知道是否安全

    a. Create new folder local.
    b. Move all project into the local folder expect public folder.
    c. Move all the content of public folder to project root.
    d. Delete the blank public folder
    f. Edit the index file. 

编辑index.php

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

to

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

我知道这个问题有很多解决方案。最好和最简单的解决方案是在根目录中添加.htaccess文件。

我尝试了我们为这个问题提供的答案,但我在Laravel中遇到了一些auth:api守卫的问题。

以下是最新的解决方案:

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

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    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>

在根目录中创建.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文件放在那里。

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

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

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

对于XAMPP用户来说,要在不接触laravel默认文件系统的情况下从url中删除public,需要为应用程序设置一个虚拟主机,只需遵循以下步骤即可

打开XAMPP控制面板应用程序并停止Apache。请注意,后期的Windows机器可能会将其作为服务运行,因此请勾选Apache模块左侧的复选框。 进入C:/xampp/apache/conf/extra或xampp文件所在目录。 使用文本编辑器打开名为httpd-vhosts.conf的文件。 在第19行左右找到# NameVirtualHost *:80并取消注释或删除散列。 在文件的最底部粘贴以下代码:

<VirtualHost *>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/xampp/htdocs">
        Options Indexes FollowSymLinks Includes ExecCGI
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

现在您可以复制并粘贴下面的代码来添加您的虚拟主机目录。例如,我正在一个名为Eatery Engine的网站上工作,所以下面的代码片段将允许我在我的本地安装上使用子域:

<VirtualHost eateryengine.dev>
    ServerAdmin admin@localhost.com
    DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder
    ServerName eateryengine.dev
    ServerAlias eateryengine.dev
    <Directory "C:/xampp/htdocs/eateryengine">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

接下来转到你的Windows主机文件编辑你的HOSTS。该文件将位于C:/Windows/System32/drivers/etc/hosts,其中hosts是文件。用记事本打开。 寻找在DNS本身中处理的#localhost名称解析。

127.0.0.1 localhost

本地主机名解析在DNS本身中处理。

127.0.0.1       localhost
127.0.0.1       eateryengine.dev #change to match your Virtual Host.
127.0.0.1       demo.eateryengine.dev #manually add new sub-domains.

重新启动Apache并测试所有内容。

原文可以在这里找到