Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5 redirect loop

I currently have a redirect loop when deploying my code on heroku but not on my local wamp server. Below is my middleware, its been properly registered. Im using laravel 5, Thanks

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Redirect;

class HttpsEnforcer
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next){   
        if(!$request->secure())
        {
            return redirect()->secure($request->path());
        }


        return $next($request);
    }
}
like image 500
alaboudi Avatar asked Nov 27 '25 04:11

alaboudi


1 Answers

Request::secure() check HTTP_X_FORWARDED_PROTO header for https, make sure they are being set.

If you are behind a load balancer which listen to 443 and send traffic on 80 with HTTP_X_FORWARDED_PROTO you will need to use Request::setTrustedProxies($LB_IP)

like image 122
Junaid Avatar answered Nov 28 '25 20:11

Junaid