Laravel 基础 url 显示非 SSL url()

Laravel base url display non SSL url()

我通过 .htaccess 启用强制 SSL。它工作正常并重定向到 .env 文件

中的 https://www.myappurl.com. Inside my application, I use Ajax to retrieve the category list. after SSL enabled category list not loading. it's shown without SSL URL http://www.myappurl.com. I also change APP_URL to https://www.myappurl.com

我的 .htaccccess 代码

RewriteEngine On

    # ensure www.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Ajax代码

 $.ajax({

        url: '{{url('getsateslist')}}',
        type: "POST",
        dataType: "json",

secure_url() 辅助函数生成完全限定的 HTTPS URL 到给定路径,更改 :

url: '{{ url('getsateslist') }}',

url: '{{ secure_url('getsateslist') }}',

参见[secure_url()]的官方文档here

看到您正在使用 X-Forwarded-Proto 来确定协议,那么我假设您的站点位于终止 SSL 请求的代理后面,并且代理本身使用 HTTP 连接到您的站点,那么您需要enable the "trusted proxies" middleware

您可以找到 basic implementation on github(它确实随 Laravel 样板一起提供),但您也可以实现自己的样板。

例如,如果您使用的是 AWS 负载均衡器,您可以执行以下操作:

<?php

namespace App\Http\Middleware;

use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB;
}

这使用 TrustedProxy 包,它是 Laravel 依赖项,因此可以在那里找到更多信息。