【问题标题】:Prevent access to Laravel through /public/ directory using .htaccess [duplicate]使用 .htaccess 防止通过 /public/ 目录访问 Laravel [重复]
【发布时间】:2019-12-13 04:19:21
【问题描述】:

我在共享主机上设置了一个 Laravel 项目,其中文档根目录不能设置为 Laravel 的“公共”文件夹。为了解决这个问题,我使用了以下 .htaccess 文件:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^habbo.gallery$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.habbo.gallery$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

这很好用。我现在可以通过“https://domain.tld”访问我的项目。但是,仍然可以通过“https://domain.tld/public/”访问该项目,这是我想要阻止的。我无法找到这个问题的答案,所以我想知道这是否可能只使用 .htaccess。

【问题讨论】:

  • 您可以尝试将 public 文件夹重命名为 public_html 或您主机中使用的任何名称。 stackoverflow.com/questions/30198669/…
  • 你可以尝试将https://domain.tld/public/重定向到https://domain.tld
  • @HTMHell 等一下..这实际上可以工作。目前所有内容都在 public_html (这是文档根目录)中,但也许我可以将所有内容上移 1 个目录,然后按照你说的做?
  • @N'Bayramberdiyev 是的,我一直在尝试,也许我做错了,但它给了我一个无限循环的重定向
  • @Metoniem 是的,这可能是一个更好的解决方案。

标签: php laravel .htaccess shared-hosting


【解决方案1】:

从公共文件夹复制两个文件并将它们粘贴到根文件夹。 两个文件是:index.php.htaccess

然后在粘贴到根文件夹的编辑器上打开index.php 文件。然后将两行替换为一些更改,如下所示。请参阅我所说的代码中的 cmets 通过以下代码替换它

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

// require __DIR__.'/../vendor/autoload.php'; Replace this by following code
require __DIR__.'/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
// $app = require_once __DIR__.'/../bootstrap/app.php'; Replace this by following code
$app = require_once __DIR__.'/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2011-05-27
    • 2011-01-21
    • 1970-01-01
    • 2018-06-27
    • 2019-03-16
    相关资源
    最近更新 更多