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


当前回答

<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

其他回答

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

原文可以在这里找到

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

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

在根目录下创建一个.htaccess文件,并粘贴下面的代码。就是P

RewriteEngine上 重写%{REQUEST_FILENAME} -d [OR] 重写%{REQUEST_FILENAME} -f RewriteRule ^ ^$1 [N] RewriteCond %{REQUEST_URI} (\.\w+$) [NC] RewriteRule ^(.*)$ public/$1 重写%{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} RewriteRule ^ server.php

你只需要2个简单的步骤就可以做到

重命名laravel项目根目录下的server.php文件。 在根目录上创建一个.htaccess文件,并将下面的代码粘贴进去

Options -MultiViews -Indexes

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

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