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


当前回答

4个最好的方法从URL删除公共。

如果您使用任何其他技巧从URL中删除公共,例如将server.php的名称更改为index.php,并更改为核心文件路径。显然,不要那样做。那为什么Laravel不给出这样的解呢因为这不是正确的方法。

1)在Laravel中使用htaccess从URL中删除公共

通过在根目录下添加一个。htaccess文件,你可以在没有public的情况下访问网站

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

2)通过在本地创建虚拟主机来删除公共

我在这里演示的是windows操作系统。但我将尝试定义一个步骤,以便任何人都可以轻松地遵循该步骤。您还可以在谷歌上研究特定操作系统的相同内容。

步骤1:进入C:\Windows\system32\drivers\etc\以管理员模式打开“hosts”文件。

步骤2:添加以下代码。在这里,我给您演示projectname。本地域名演示,您可以指定任何您喜欢的。让每个地方都保持不变。

127.0.0.1  projectname.local

第三步:现在转到C:\xampp\apache\conf\extra,对于xampp用户和wamp用户,“C:\wamp\bin\apache\Apache2.4.4\conf\extra”,打开“httpd-vhosts.conf”文件。现在将以下代码添加到其中。

注意:根据您的项目更改文档根,并将域名添加到“hosts”文件中。

<VirtualHost projectname.local>
      ServerAdmin projectname.local
      DocumentRoot "C:/xampp/htdocs/projectdir"
      ServerName projectname.local
      ErrorLog "logs/projectname.local.log"
      CustomLog "logs/projectname.local.log" common
 </VirtualHost>

第四步:最后但重要的一步是重新启动Xampp或Wamp并访问http://projectname.local这样的url,您的Laravel将在没有公共url的情况下响应。


3)在Laravel运行命令移除公众

如果你在本地工作,那么你不需要做任何事情,只需要从你的终端或命令行工具运行以下命令。在此之后,您可以通过命令行提供的URL访问您的网站。

   > php artisan serve

如果你想在特定的IP上运行你的项目,那么你需要运行以下命令。如果你在局域网工作,那么如果你想允许其他人从本地访问你的网站,那么你只需要在得到你的IP地址后,使用命令行通过运行“ipconfig”检查你的IP地址。

> php artisan serve --host=192.168.0.177

如果您希望在特定IP和特定端口上运行项目,则需要执行以下命令。

> php artisan serve --host=192.168.0.177 --port=77

4)删除托管服务器或cpanel上的公共

项目完成后,您需要在服务器上托管项目,然后您只需要将域上的文档根目录设置为公共文件夹。查看下面的截图。

根据截图,如果你在public_html中没有任何项目文件夹,那么你只需要将你的文档根目录设置为“public_html/public”。

此处引用

其他回答

我在这里尝试了一些解决方案,发现htaccess适用于laravel 5.2,像这样:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]



RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
RewriteRule ^.env - [F,L,NC]
RewriteRule ^app - [F,L,NC]
RewriteRule ^bootstrap - [F,L,NC]
RewriteRule ^config - [F,L,NC]
RewriteRule ^database - [F,L,NC]
RewriteRule ^resources - [F,L,NC]
RewriteRule ^vendor - [F,L,NC]

当你有另一个文件或文件夹禁止只需添加

RewriteRule ^your_file - [F,L,NC]

即使我不建议把Laravel放在根文件夹上,但在某些情况下它是不可避免的; 对于这些情况下,上述方法并不适用于资产,所以我做了一个快速修复改变htaccess: 将server.php复制到index.php后,编辑.htaccess文件,如下所示:

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

    RewriteEngine On

    ### fix file rewrites on root path ###
    #select file url
    RewriteCond %{REQUEST_URI} ^(.*)$
    #if file exists in /public/<filename>
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
    #redirect to /public/<filename>
    RewriteRule ^(.*)$ public/$1 [L]
    ###############

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

    # Handle Front Controller...

    #RewriteCond %{REQUEST_FILENAME} -d # comment this rules or the user will read non-public file and folders!
    #RewriteCond %{REQUEST_FILENAME} -f #
    RewriteRule ^ index.php [L]
</IfModule>

这是一个快速修复我必须使任何人都欢迎升级它。

我知道这个问题有很多解决方案。最好和最简单的解决方案是在根目录中添加.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文件并添加以下代码。一切都很顺利

对于拉拉维尔 5:

将Laravel根文件夹中的server.php重命名为index.php 从/public目录复制.htaccess文件到你的Laravel根目录 文件夹中。

就是这样!

在根项目文件夹中创建一个名为.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>

注意:如果你将server.php更改为index.php,你也应该在上面的代码中重命名它