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


当前回答

https://github.com/dipeshsukhia/laravel-remove-public-url

你可以使用这个包

编译器需要dipeshsukhia/ laravell -remove-public-url——dev

php artisan vendor:publish——tag=LaravelRemovePublicUrl

其他回答



请注意,当使用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文件夹。

这是所有

即使我不建议把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>

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

对于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并测试所有内容。

原文可以在这里找到

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

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

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

在根目录中创建.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>