我知道这是一个非常流行的问题,但我还没有找到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文件到你的根文件夹,粘贴下面的代码,用你自己的域名替换yourdomainname

RewriteEngine上 重写HTTP_HOST} ^(www.) ^/public/ .重写%{REQUEST_URI}

RewriteCond %{REQUEST_FILENAME} 重写%{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /public/$1

重写HTTP_HOST} ^(www.) RewriteRule ^ (/) ?$ /public/index.php

其他回答

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

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

以下是截至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!

你不需要做太多,只需要在根目录上创建一个.htaccess文件,然后粘贴/保存下面的文件;

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteRule ^(.*)$ public/$1 [L]
    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>

激活mod_rewrite模块

sudo a2enmod rewrite

重新启动apache

sudo service apache2 restart

要在.htaccess文件中使用mod_rewrite(这是一个非常常见的用例),请使用

sudo nano /etc/apache2/sites-available/000-default.conf

在“DocumentRoot /var/www/html”下面添加以下行:

<Directory "/var/www/html">
AllowOverride All
</Directory>

重新启动服务器:

sudo service apache2 restart


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

这是所有