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


当前回答

我用2个答案解决了这个问题:

Renaming the server.php to index.php (no modifications) Copy the .htaccess from public folder to root folder (like rimon.ekjon said below) Changing .htaccess it a bit as follows for statics: 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 ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] If there are any other static files needed just add the extension to the previous declared list

其他回答

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

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

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

以下是截至2018年5月对我有效的最佳和最短的解决方案 Laravel 5.5

just cut your .htaccess file from the /public directory to the root directory and replace it content with the following code: <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> 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 [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] </IfModule> Just save the .htaccess file and that is all. Rename your server.php file to index.php. that is all enjoy!

我用2个答案解决了这个问题:

Renaming the server.php to index.php (no modifications) Copy the .htaccess from public folder to root folder (like rimon.ekjon said below) Changing .htaccess it a bit as follows for statics: 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 ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC] If there are any other static files needed just add the extension to the previous declared list

1)我还没有找到L5中公共目录移动的工作方法。虽然可以修改bootstrap index.php中的一些内容,但似乎有几个辅助函数是基于公共目录存在的假设。总之,老实说,你真的不应该移动公共目录。

2)如果你使用MAMP,那么你应该为每个项目创建新的vhosts,每个服务于项目的公共目录。创建后,您可以通过定义的服务器名访问每个项目,如下所示:

http://project1.dev
http://project2.dev


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

这是所有