ChatGPT解决这个技术问题 Extra ChatGPT

Laravel 5 - 从 URL 中删除公共

我知道这是一个非常受欢迎的问题,但我一直无法为 Laravel 5 找到可行的解决方案。我一直在尝试从 Codeigniter 迁移很长时间,但是这个复杂的安装过程让我望而却步。

我不想运行虚拟机,这在项目之间切换时似乎很尴尬。

我不想将我的文档根目录设置为公用文件夹,这在项目之间切换时也很尴尬。

我试过 .htaccess mod_rewrite 方法

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

这只是在compiled.php第7610行给了我一个Laravel NotFoundHttpException。

前阵子尝试L4的时候,使用了将public文件夹的内容移动到根目录的方法。 L5 的结构完全不同,遵循相同的步骤完全破坏了 Laravel(服务器只会返回一个空白页面)。

是否有一种在开发环境中删除“公共”的体面方法:

与 L5 一起工作 让我可以轻松地在项目之间切换(我通常在任何时候都在处理 2 或 3 个)。

谢谢

** 我正在使用 MAMP 和 PHP 5.6.2

指南中的文件夹结构与我的不同,我想他没有使用L5?我省略了他对 Bootstrap/Paths 文件所做的更改,因为它不存在。该项目似乎正在运行。你觉得这样好吗?
我的错误让我为 L5 添加答案
尝试相同没有成功
它似乎只通过修改 index.php 文件中的路径来工作,但我是 Laravel 的新手,所以显然无法评论这是否稳定/安全。
其他文件夹/文件应该在您的文档根目录下。

P
Prakash Pazhanisamy

对于 Laravel 5:

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

而已!


请查看仅在根目录中创建 .htacess 文件的其他答案,因为此答案可能会暴露敏感信息(如 .env 文件)
您的站点可供黑客使用 cos .env 文件,我们可供所有人查看。
这是不安全的。
继续滚动以获得更好的答案。
这是一个令人难以置信的糟糕答案,并且可能是被黑 Laravel 网站的第一大来源。
D
Derk Jan Speelman

请注意在使用 Docker 为 Laravel 项目提供服务时:您无需执行任何操作。 仅当您网站的根(或更常见:public_html)目录是您的 Laravel 项目时才使用此选项(使用 Docker 时不是这种情况)。

不!

您真的不应该将 Laravel 根文件夹中的 server.php 重命名为 index.php 并将 .htaccess 文件从 /public 目录复制到您的 Laravel 根文件夹!!!

这样,每个人都可以访问您的某些文件(例如 .env)。自己试试。你不想要那个!

相反,您应该在根目录中创建一个 .htaccess 文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

这将静默地将您的所有基本 URI 重写到 /public 文件夹。甚至所有 Headers(例如 the HTTP Authorization Header)和所有可选的 URI 参数也将静默传递到 /public 文件夹。

就这样


它在生产中运行良好,但在本地没有任何建议?
这是完美的工作!本地和服务器感谢安全帮助!
您好,@DerkJanSpeelman 我正在尝试将我的 laravel 文件上传到我的主机中的子文件夹中,例如:(example.com/projects/laravel/blog)在这种情况下,访问者必须访问 example.com/projects/laravel/blog/公开的,以便我将带有您的代码的 .htaccess 文件放在博客文件夹根目录中,并希望它重定向到公共文件夹。但它不起作用。这是发生的错误:在此服务器上找不到请求的 URL /public/。我该如何解决这个问题?
这个答案被低估了。支持粗体并突出显示不要。它工作得很好。
完美的答案
H
Haseeb Zulfiqar

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

将 server.php 重命名为 index.php(无修改) 将 .htaccess 从公用文件夹复制到根文件夹(如下面的 rimon.ekjon 所述) 将 .htaccess 更改为静态如下: 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] 如果需要任何其他静态文件,只需将扩展名添加到先前声明的列表中


R
Raham

在 Laravel 5.5 中,在您的根目录中创建 .htacess 文件并放置以下代码:- Reference Link

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

最好的答案!在我的示例中唯一有效的,它肯定应该有更多的赞成票
谢谢这对我有用。但我想知道它是否有任何安全问题。
这条规则 RewriteRule ^ ^$1 [N] 有什么作用?
在链式视图资源上,例如带有 admin.something.something 的仪表板不起作用。必须对路由文件进行任何修改吗?
@LuizWynne 看看 my answer
U
Udhav Sarvaiya

root 目录中创建 .htaccess 文件并放置如下代码。

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

我真的推荐这个解决方案。
我也推荐这个解决方案
太感谢了。这是完美的(y)
它工作正常,但仍然可以直接从 URL 访问 .env 文件。
这是一个安全的解决方案吗?
M
Muhammad Sadiq

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

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

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


在没有加载css之后,bcoz所有css都是公开的,并且从url中删除了public
您可以在加载时将“public/”放在 js 或 css 文件之前,例如 Html::script("public/js/....."); --但最好的解决方案是虚拟主机......
M
Miron

@rimon.ekjon 说:

将 Laravel 根文件夹中的 server.php 重命名为 index.php 并将 .htaccess 文件从 /public 目录复制到 Laravel 根文件夹。 - 而已 !! :)

这对我有用。但是 /public 目录中的所有资源文件都找不到并且请求 url 不起作用,因为我使用了asset() 助手。

我更改了 /Illuminate/Foundation/helpers.php/asset() 函数如下:

function asset($path, $secure = null)
{
    return app('url')->asset("public/".$path, $secure);
}

现在一切正常:)

谢谢@rimon.ekjon 和你们所有人。

2020作者更新

不推荐这个答案。相反,建议处理 .htaccess 文件。


这不是解决此问题的好方法,因为您尝试在供应商目录中进行更新,这不是好的做法。
您只需在从公共目录复制的 .htaccess 文件中添加 RewriteRule ^(.*)$ public/$1 [L],方法是删除此行 RewriteRule ^(.*)/$ /$1 [L,R=301]
A
Abhinav Saraswat

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

如果您想将代码保留在子目录中,那么您可以创建一个子域,那么此代码也适用于此。


在阅读您的答案之前,我在下面回答了,这必须是公认的答案。没有文件更改:只是一个简单、聪明的规则。
@Raham 您当前的网址是什么?
只是想在这位先生的作品之上添加一个简短的描述。上面的代码适用于根 public_html 文件夹(我也有问题)。说过你的核心 laravel 文件应该在 public_html 文件夹中,我犯了一个错误,如果你把上面的代码放在 laravelapp 中,我的目录看起来像 public_html/laravelapp/public 它不会工作。因此,您必须将所有核心文件复制到 public_html 并将 .htaccess 文件放在那里。参考 laravel 的 centOS 和 Cpanel 托管。
使用这种方法有什么缺陷?
对我来说就像一个魅力,我只有 cPanel webshost,无法更改 Apache 的 RootDirectory。谢谢 !最简单最安全。这将不允许查看您的根文件夹内容
J
Jignesh Joisar

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

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

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

J
Jignesh Joisar

可以在 laravel5 中删除公共 url。按着这些次序:

步骤1.从公共复制所有文件并粘贴到根目录

步骤 2.open index.php 文件替换为

 require __DIR__.'/../bootstrap/autoload.php';

 require __DIR__.'/bootstrap/autoload.php';

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

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

并删除所有缓存和 cookie。


嗨,我已经尝试过您的方法,执行了上述步骤,但它显示以下错误: 警告:require_once(D:\Projects\laravel/public/index.php): failed to open stream: No such file or directory in D: \Projects\laravel\server.php 在第 21 行致命错误:require_once(): Failed opening required 'D:\Projects\laravel/public/index.php' (include_path='.;C:\xampp\php\PEAR' ) 在第 21 行的 D:\Projects\laravel\server.php
这不是一个完美的答案,而是一种非常不安全的方式。
这是非常糟糕的建议
M
Mayank Dudakiya

从 URL 中删除公共的 4 种最佳方法。

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

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

通过将 .htaccess 文件添加到根目录中,您可以在不公开的情况下访问该网站

<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)通过在本地创建虚拟主机来移除公众

我在这里为 Window 操作系统提供演示。但我会尝试定义一个步骤,以便任何人都可以轻松地遵循该步骤。您也可以针对特定操作系统在 google 上进行研究。

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

第 2 步:将以下代码添加到其中。在这里,我给你一个projectname.local域名demo的demo,你可以任意指定。只要让它在每个地方都保持不变。

127.0.0.1  projectname.local

第 3 步: 现在转到 C:\xampp\apache\conf\extra(xampp 用户和 wamp 用户 "C:\wamp\bin\apache\Apache2.4.4\conf\extra")并打开 "httpd-vhosts.conf" 文件。现在将以下代码添加到其中。

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

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

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

3) 通过在 Laravel 中运行命令来移除 public

如果您在本地工作,则无需执行任何操作,只需从终端或命令行工具运行以下命令即可。之后,您可以通过命令行提供的 URL 访问您的网站。

   > php artisan serve

如果您愿意在特定 IP 上运行您的项目,那么您需要运行以下命令。如果您在 LAN 上工作,那么如果您想允许其他人从本地访问您的网站,那么您只需在按照命令运行 IP 地址后运行“ipconfig”,使用命令行检查您的 IP 地址。

> php artisan serve --host=192.168.0.177

如果您愿意在具有特定端口的特定 IP 上运行您的项目,那么您需要执行以下命令。

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

4)在托管服务器或cpanel上删除公众

https://i.stack.imgur.com/RCr8O.png

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

根据屏幕截图,如果您在 public_html 中没有任何项目文件夹,那么您只需将文档根目录设置为 "public_html/public"

参考来自 here


所有四种方法都很好,但我已经通过使用第一个解决方案解决了我的问题。
@MayankDudakiya 如果 laravel 6 应用程序没有加载 css js 并且当你执行 ctrl + u 时你会怎么做,你会看到这个 i.postimg.cc/R00GQkXQ/Untitled.jpg
@Umair 根据您的屏幕截图,您的路径中有一个错误。它包含在 /public//public 中。只需尝试从您的 asset()url() 函数中删除 /public/
@MayankDudakiya 这是因为我在 .env 文件中有 ASSET_URL=public ,但即使在删除它之后,css和js文件仍然没有加载。网络选项卡显示 404。我还尝试了 {{ URL::asset('css/style.css') }} 和 {{asset('css/style.css') }} 还尝试了不同的 htaccess 文件。还尝试从根目录中删除 htaccess 和索引。权限也是正确的。我已经设置了许多 laravel 应用程序并遇到了这个问题,并且不知何故我让它们工作了,但我不知道该怎么做。所有文件都在那里,但没有加载。
@MayankDudakiya 这是控制台i.postimg.cc/BnWZsS2k/cmate-console.jpg
U
Udhav Sarvaiya

这是截至 2018 年 5 月适用于 Laravel 5.5 的最佳和最短的解决方案

只需将 .htaccess 文件从 /public 目录剪切到根目录,并将其内容替换为以下代码: Options -MultiViews 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] 只需保存 .htaccess 文件即可。将 server.php 文件重命名为 index.php。这就是享受!


A
Angelo Joseph Salvador

假设您将所有其他文件和目录放在名为“locale”的文件夹中。

https://i.stack.imgur.com/k8qpP.png

只需转到 index.php 并找到这两行:

require __DIR__.'/../bootstrap/autoload.php';

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

并将它们更改为:

require __DIR__.'/locale/bootstrap/autoload.php';

$app = require_once __DIR__.'/locale/bootstrap/app.php';

W
Wasim A.

最佳方法:我不建议删除 public,而是删除 on local computer create a virtual host point to public directoryon remote hosting change public to public_html and point your domain to this directory。原因,你的整个 laravel 代码将是安全的,因为它的一级到你的公共目录:)

方法一:

我只是将 server.php 重命名为 index.php 就可以了

方法二:

这适用于所有 laravel 版本...

这是我的目录结构,

/laravel/
... app
... bootstrap
... public
... etc

按照这些简单的步骤

现在将所有文件从公共目录移动到根 /laravel/,不需要公共目录,因此您现在可以选择删除它现在打开 index.php 并进行以下替换

需要 DIR.'/../bootstrap/autoload.php';

需要 DIR.'/bootstrap/autoload.php';

$app = require_once DIR.'/../bootstrap/start.php';

$app = require_once DIR.'/bootstrap/start.php';

现在打开 bootstrap/paths.php 并更改公共目录路径:

'public' => DIR.'/../public',

'public' => DIR.'/..',

就是这样,现在试试 http://localhost/laravel/


一切都好。但工匠命令不起作用,只是显示“无法打开输入文件:工匠”。有什么建议吗?
此方法有效,如果您可以向用户公开您的数据库凭据。 localhost/laravel/.env
S
Sean Perkins

我想添加到@Humble Learner 并注意“修复”资产的 url 路径的正确位置是 /Illuminate/Routing/UrlGenerator.php/asset()。

更新匹配的方法:

public function asset($path, $secure = null)
{
    if ($this->isValidUrl($path)) return $path;
    $root = $this->getRootUrl($this->getScheme($secure));
    return $this->removeIndex($root).'/public/'.trim($path, '/');
}

这将修复脚本、样式和图像路径。任何资产路径。


p
pirho

对于 XAMPP 用户从 url 中删除公共而不接触 laravel 默认文件系统是为您的应用程序设置一个虚拟主机来执行此操作,请按照以下步骤操作

打开 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 主机文件以编辑您的主机。该文件将位于 C:/Windows/System32/drivers/etc/hosts,其中 hosts 是文件。用记事本打开它。查找 #localhost 名称解析是在 DNS 本身内处理的。

127.0.0.1 localhost

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 并测试一切。

可以在 here 找到原始文章


R
Reiah Paul Sam

在 Laravel 设置中总是有一个公共文件夹的原因,所有公共相关的东西都应该存在于公共文件夹中,

不要将您的 IP 地址/域指向 Laravel 的根文件夹,而是将其指向公共文件夹。将服务器 Ip 指向根文件夹是不安全的,因为除非您在 .htaccess 中写入限制,否则可以轻松访问其他文件。,

只需在.htaccess文件中写入重写条件并安装重写模块并启用重写模块,在路由中添加public的问题就解决了。


A
Abd Abughazaleh

在根项目文件夹中创建名为 .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,您还应该在上面的代码中重命名它


m
mujuonly
<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(这是一个非常常见的用例),请使用以下命令编辑默认 VirtualHost

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

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

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

再次重启服务器:

sudo service apache2 restart

P
Paul De Zwaan

问题是如果您键入 /public 并且它仍然可以在 url 中使用,因此我创建了一个应该放在 public/index.php 中的修复程序

  $uri = urldecode(
     parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  );

  if(stristr($uri, '/public/') == TRUE) {

    if(file_exists(__DIR__.'/public'.$uri)){

    }else{

      $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
      $actual_link = str_replace('public/', '',$actual_link);
      header("HTTP/1.0 404 Not Found");
      header("Location: ".$actual_link."");
      exit();
      return false;
   }}

这种和平的代码将从 url 中删除 public 并给出 404 然后重定向到没有 public 的 url


C
CHARITRA SHRESTHA

我知道对于这个问题有很多解决方案。最好和最简单的解决方案是在根目录中添加 .htaccess 文件。

我尝试了我们为这个问题提供的答案,但是我在 Laravel 中遇到了 auth:api guard 的一些问题。

这是更新的解决方案:

<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 文件并添加此代码。一切顺利


G
Ghanshyam Nakiya

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

n
newbdeveloper

在根目录中创建一个 .htaccess 文件并粘贴以下代码。就是这样:P

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


我尝试了很多解决方案。但这对我来说就像魔术一样。这是被高度低估的答案,我对此表示赞同。所有觉得这个答案有帮助的人,请点赞。谢谢@newbdeveloper
P
Plokko

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

这是我必须做的一个快速修复,所以欢迎任何人升级它。


B
Brigo

拉拉维尔 5.5

第一次安装 Laravel 后,我遇到了著名的“公用文件夹问题”,我想出了这个解决方案,在我个人看来,它比我在 Web 上找到的其他解决方案“更干净”。

成就

URI 中没有公共字词

保护 .env 文件免受好奇的人的攻击

只需使用 mod_rewrite 和四个简单规则编辑 .htaccess 即可完成所有操作。

脚步

将 .htaccess 文件移至主根目录的 public/.htaccess 中编辑如下

我评论了所有内容,所以对于那些从未使用过 mod_rewrite 的人来说(我希望)也应该清楚(不是说我是专家,恰恰相反)。另外,为了理解规则,必须清楚的是,在 Laravel 中,如果 Bill 连接到 https://example.com,则加载 https://example.com/index.php。该文件只包含命令 header("refresh: 5; https://example.com/public/"),它将请求发送到 https://example.com/public/index.php。第二个 index.php 负责加载控制器和其他东西。

# IfModule prevents the server error if the app is moved in an environment which doesn’t support mod_rewrite
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # RULES ORIGINALLY IN public/.htaccess ---
    # 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]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    # --- END

    # PERSONAL RULES ---
    # All the requests on port 80 are redirected on HTTPS
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

    # When .env file is requested, server redirects to 404
    RewriteRule ^\.env$ - [R=404,L,NC]

    # If the REQUEST_URI is empty (means: http://example.com), it loads /public/index.php
    # N.B.: REQUEST_URI is *never* actually empty, it contains a slash that must be set as match as below
    # .* means: anything can go here at least 0 times (= accepts any sequence of characters, including an empty string)
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*) /public/index.php [L]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI !== css||js||images/whatever => server loads /public/index.php, which is responsible to load the app and the related controller
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^(css|js|images|media)/(.*)$ /public/index.php [L,NC]

    # If the current request is asking for a REQUEST_FILENAME that:
    # a) !== existent directory
    # b) !== existent file
    # => if URI == css||js||images[=$1]/whatever[=$2] => server loads the resource at public/$1/$2
    # If R flag is added, the server not only loads the resource at public/$1/$2 but redirects to it
    # e.g.: bamboo.jpg resides in example.com/public/media/bamboo.jpg
    #       Client asks for example.com/media/bamboo.jpg
    #       Without R flag: the URI remains example.com/media/bamboo.jpg and loads the image
    #       With R flag: the server redirects the client to example.com/public/media/bamboo.jpg and loads the image
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
    # --- END

</IfModule>

可以删除以下规则(最初在 public/.htaccess 中)。事实上,同样的规则在最后两条规则中以更详细的方式进行了阐述。

# Handle Front Controller...
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^ index.php [L]

编辑:我错过了 Abhinav Saraswat's solution,他的回答应该是被接受的。只有一个简单明了的规则,将所有流量重定向到公用文件夹而不修改任何文件。


C
Community

首先,您可以使用此步骤

对于 Laravel 5: 1. 将 Laravel 根文件夹中的 server.php 重命名为 index.php 2. 将 .htaccess 文件从 /public 目录复制到 Laravel 根文件夹。

来源:https://stackoverflow.com/a/28735930

按照这些步骤操作后,您需要更改所有 css 和脚本路径,但这会很累。

解决方案建议:只需对 helpers::asset 函数进行小幅改动即可。

为了这:

打开 vendor\laravel\framework\src\Illuminate\Foundation\helpers.php 转到第 130 行写“public/”。$path 而不是 $path,函数资产($path,$secure = null){ return app('url' )->asset("public/".$path, $secure); }


规则 1:永远不要编辑核心文件。即使它现在看起来简单而美好,你也会在以后付款。
@JanakaDombawela 我不这么认为,因为:
@JanakaDombawela 我不这么认为,因为:Senerio 1:如果开发人员有经验:开源代码适合这项工作 Senerio 2:如果开发人员是初级人员,开发人员必须对源代码进行编辑以获得经验
Y
Yousef Altaf

我以前读过一些文章,它工作正常,但真的不知道是否安全

    a. Create new folder local.
    b. Move all project into the local folder expect public folder.
    c. Move all the content of public folder to project root.
    d. Delete the blank public folder
    f. Edit the index file. 

编辑 index.php

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

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

E
Eli Makumba

我使用的另一种方法是使用 htdocs 或 www 中的 ln 命令创建符号链接(在 Linux 中,不知道 Win),即 ln projectname/public project 该站点因此可以通过 localhost/project 访问


V
Vinod Joshi

您可以使用各种方法从 url 中删除 public 关键字。

1) 如果您使用的是专用主机并且您具有 root 访问权限,那么您可以使用虚拟主机从 url 中删除 public 关键字。您应该为 DocumentRoot 提供公共路径。所以这将从公共目录开始索引并将其从 url 中删除。

<VirtualHost *:80>
    ServerAdmin info@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/{yoursourcedirectory}/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2)如果您没有主机的根访问权限,那么您应该在根目录中生成一个新的 .htaccess 文件并将代码如下

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

您可以在此处获得更多reference


d
dipenparmar12

您只需 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]