我有一个 Laravel 9 应用程序,部署在
hostinger
共享托管。
我的网站文件看起来像这样
public_html/laravel-app
。
在 public_html 文件中,我没有
index.php
文件来更改链接到访问 Laravel 应用程序所需的引导程序。因此,当前所有请求如下所示:
domain/laravel-app/public/index.php/{ROUTE}
。
我该如何修复它,使其看起来像:
domain/{ROUTE}
?
您这里有两个问题:
您网站的文档根目录设置为比您的应用程序所在位置高两级
您没有正确配置重写。
andylondon 的答案将帮助您解决问题#2,但是如果您的目录可见性设置不正确,这仍然是一个很大的安全风险。您的 .env 文件和您可能以纯文本形式存在的任何其他配置文件都可以从网络访问,可能会暴露敏感信息。
由于您使用的是 cPanel,因此您无法更改主域的文档根目录。您可以使用的解决方案是将
laravel-app
文件夹移动到您的主目录,比 public_html
高一级。然后,将 laravel-app/public
文件夹的内容移动到 public_html
中。您需要编辑 Laravel 的 index.php
中使用的路径,因为您的应用程序不在网络无法访问的文件夹中。
如果您将
laravel-app
放在主目录中,并将 Laravel 的 public
文件夹的内容放在 public_html
中,则您的 index.php
文件(从 Laravel 9 开始)应如下所示:
<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/
if (file_exists($maintenance = __DIR__.'/../laravel-app/storage/framework/maintenance.php')) {
require $maintenance;
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/../laravel-app/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/
$app = require_once __DIR__.'/../laravel-app/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
确保您在 cPanel 的文件浏览器中启用隐藏文件,并确保
.htaccess
文件也在那里,以解决您的重写问题。
这对你来说是一个很大的安全漏洞
您可以尝试在根目录中添加.htaccess文件
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
我会向您的托管主机寻求帮助,因为他们可以更安全地为您完成此操作。
首先,我将此归功于@apokryfos、@user83129 和@andylondon。解决方案是结合使用这些帮助。
.htaccess
安全文件,如 @andylondon 所说,但应该如下所示 (added laravel-app before public)
:RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /laravel-app/public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /laravel-app/public/index.php [L]
laravel-app/public
的内容复制到public_html
public_html/index.php
使其看起来完全像这样:<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/
if (file_exists($maintenance = __DIR__.'/store-api-9/storage/framework/maintenance.php')) {
require $maintenance;
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/laravel-app/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/
$app = require_once __DIR__.'/laravel-app/bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
PS:我的答案中添加的
laravel-app
是一个自定义名称,我在其中将包含部署的Laravel应用程序的目录命名为public_html
,并且可以根据每个开发人员进行更改。
已在28364496中回答。我的博客在 Laravel 中没有在 CPanel 中公开 - How。 请确保.env 不可公开访问。
因此,将laravel项目的.htaccess文件移动到/public_html/.htaccess。
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# 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 ^ %your_project_path%/public/index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
%your_project_path% 应替换为实际项目路径。就像 domain/larave-app 一样。
此方法效果最佳:上传 Laravel 项目的 zip 文件,然后将数据库上传到 PHPMyAdmin,并使用指定的代码创建一个 .htaccess 文件。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>