我有一个项目,我在 ubuntu 服务器上安装了 SSL 证书。我已完成找到的所有解决方案,但每次打开页面时仍会收到此消息。
The requested URL /about was not found on this server.
这是我为强制 Laravel 使用 https 所做的更改:
1- 我更改了公用文件夹中的 .htaccess 并添加了这些行
# Added to Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
2- 在 app->Providers->AppServiceProvider 中,我已将其添加到 boot() 函数
if (App::environment() === 'production' || App::environment() === 'dev') {
URL::forceScheme('https');
}
3- 我创建了 php artisan make:middleware ForceSSL 并将以下代码添加到句柄函数
if (!$request->secure() && in_array(env('APP_ENV'), ['stage', 'production'])) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
在 Kernal.php 中
\MyApp\Http\Middleware\ForceSSL::class
在 .env 文件中,我已将 APP_URL 更改为 https:// 我还更改了配置文件夹内 app.php 中的 APP_URL。
我在这里遗漏了什么?这两天我不明白为什么:(
最佳答案
您需要做一些事情来确保正常工作:
- 您的路由是否在标准 HTTP 模式下工作且 url 中没有
index.php
。就像有时 laravel.com/aboutus 不起作用但 laravel.com/index.php/aboutus 有效。如果是这种情况,您需要在 php 中启用mod_rewrites
,然后在您的虚拟主机配置中添加AllowOverride All
并重新启动 apache。 - 使用 HTTPS,您在 laravel 中设置的内容将使 laravel 请求转发到安全的 HTTPS url。但是,您的服务器必须能够监听、处理和响应 HTTPS 请求。
- 您需要启用 yoru 虚拟主机以监听端口
443
,这是 SSL 端口。此外,如果您有 SSL 证书,则还需要配置这些证书。 - 在了解 laravel 是否在 HTTPS 上工作之前,制作一个简单的 php 文件并尝试使用服务器 url 的 HTTPS 访问它。如果可行,那么您可以检查 laravel 中有什么问题。如果没有,则说明您的服务器上没有正确配置 SSL。
- 最后,检查
.htaccess
重写条件。 php artisan config:clear
和php artisan route:cache
希望这有助于调试
关于php - Laravel 使用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54684960/