Skip to content

Instantly share code, notes, and snippets.

@ilhamsj
Last active July 13, 2023 02:08
Show Gist options
  • Save ilhamsj/819369a56bb983eac018c5f4e04cda50 to your computer and use it in GitHub Desktop.
Save ilhamsj/819369a56bb983eac018c5f4e04cda50 to your computer and use it in GitHub Desktop.
Force HTTPS laravel on heroku

Force HTTPS laravel on heroku

add this line after RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

app\Http\Middleware\TrustProxies.php

Before
protected $proxies;

After
protected $proxies = "*";

Documentation https://laravel.com/docs/5.8/requests#configuring-trusted-proxies

@taist24
Copy link

taist24 commented Jul 21, 2020

Thanks! it works!

@eliseuborges
Copy link

Thanks, it works!

@abedmaatalla
Copy link

Not working.
Using Laravel version 5.8 on Heroku.

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

class HttpsProtocol extends Middleware{

protected $proxies = "*";
/**
 * The headers that should be used to detect proxies.
 *
 * @var string
 */
protected $headers = Request::HEADER_X_FORWARDED_ALL;

public function handle($request, Closure $next)
{

    if (!$request->secure() ) {
        Request::setTrustedProxies([$request->getClientIp()],Request::HEADER_X_FORWARDED_ALL);
        return redirect()->secure($request->getRequestUri());
    }

    return $next($request); 
}

}

`

@ilhamsj
Copy link
Author

ilhamsj commented Nov 4, 2020

Not working.
Using Laravel version 5.8 on Heroku.

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

class HttpsProtocol extends Middleware{

protected $proxies = "*";
/**
 * The headers that should be used to detect proxies.
 *
 * @var string
 */
protected $headers = Request::HEADER_X_FORWARDED_ALL;

public function handle($request, Closure $next)
{

    if (!$request->secure() ) {
        Request::setTrustedProxies([$request->getClientIp()],Request::HEADER_X_FORWARDED_ALL);
        return redirect()->secure($request->getRequestUri());
    }

    return $next($request); 
}

}

`

did you change .htaccess too ?

@luqman-v1
Copy link

thank worked on my heroku

@benisi
Copy link

benisi commented Jul 7, 2021

Thanks, nice!

@usuga1931
Copy link

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment