Laravel 将 Http 重定向到 Https

Ram*_*hov 4 php https http laravel

我们使用 Laravel 5。为了将 http 连接重定向到 https 使用中间件 HttpsProtocol。

namespace MyApp\Http\Middleware;

use Closure;

class HttpsProtocol {

    public function handle($request, Closure $next)
    {
            if (!$request->secure() && env('APP_ENV') === 'prod') {
                return redirect()->secure($request->getRequestUri());
            }

            return $next($request); 
    }
}
Run Code Online (Sandbox Code Playgroud)

在我们的 4 个测试用例中,只有 1 个正确工作(最后一次重定向)。其他 3 种情况中间件添加 url 额外的 index.php。

http://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php http://aqualink.az/index.php ---> https ://aqualink.az/index.php/index.php https://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php https: //aqualink.az/index.php ---> https://aqualink.az/index.php

Ram*_*hov 5

我用 htaccess 解决了

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)