我有一個關于在 laravel 中強制使用 HTTPS 的問題,我在 AppServiceProvider.php 中添加了一個條件,創建了一個中間件并修改了 .htaccess 檔案。但是我仍然可以訪問http頁面。有沒有其他方法可以讓 Laravel 重定向到 https 而不是 http,以及如何防止用戶訪問 http 地址?謝謝你!
我的 .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
AppServiceProvider.php :
public function boot()
{
if (env('APP_ENV') === 'production') {
$this->app['request']->server->set('HTTPS','on'); // this line
URL::forceSchema('https');
}
}
HttpsProtocol 中間件:
public function handle(Request $request, Closure $next)
{
if (!$request->secure() && app()->environment('production')) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
uj5u.com熱心網友回復:
將此添加到.htaccess檔案頂部
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/372647.html
