使用 .htaccess 阻止通过 /public/ 目录访问 Laravel

Met*_*iem 5 php .htaccess shared-hosting laravel

我在共享主机上设置了一个 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]
Run Code Online (Sandbox Code Playgroud)

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

Sau*_*ris 0

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

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

<?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);

Run Code Online (Sandbox Code Playgroud)