首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防止使用.htaccess通过/public/目录访问Laravel

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

Stack Overflow用户
提问于 2019-08-05 15:48:27
回答 1查看 1.6K关注 0票数 4

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

代码语言:javascript
运行
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2019-08-05 17:26:04

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

然后在粘贴在根文件夹上的编辑器上打开index.php文件。然后用一些更改替换两行,如下所示。参见关于代码的注释,我说过将其替换为以下代码

代码语言:javascript
运行
复制
<?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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57362030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档