Skip to content

Instantly share code, notes, and snippets.

@miyasinarafat
Last active February 25, 2019 09:24
Show Gist options
  • Save miyasinarafat/88e439cbaf8a2de1cc0e92e01229bd17 to your computer and use it in GitHub Desktop.
Save miyasinarafat/88e439cbaf8a2de1cc0e92e01229bd17 to your computer and use it in GitHub Desktop.
If you are using CloudFlare or a similar CDN you will get an infinite loop error with the %{HTTPS} solutions provided here. If you're a CloudFlare user you'll need to use this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\App;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
if (!request()->secure() && App::environment() === 'production') {
URL::forceScheme('https');
return redirect()->secure(request()->getRequestUri());
}
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
# force https
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
class HttpsProtocol
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->secure() && App::environment() === 'production') {
URL::forceScheme('https');
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment