Force Laravel to use HTTPS version Force Laravel to use HTTPS version laravel laravel

Force Laravel to use HTTPS version


Try this, go to AppServiceProvider place this code in the boot method:

\URL::forceScheme('https');

The class:

namespace App\Providers;use Illuminate\Support\Facades\URL;use Illuminate\Support\ServiceProvider;class AppServiceProvider extends ServiceProvider{    public function boot()    {        URL::forceScheme('https');    }}

Other way is enforce by .htaccess:

RewriteEngine OnRewriteCond %{HTTPS} !onRewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Remember about clearing cache in browser (then htaccess will work correctly).

Good luck!


If you want to generate https routes only (but still plain http requests allowed), change AppServiceProvider:

class AppServiceProvider extends ServiceProvider{    public function boot()    {        URL::forceScheme('https');    }}

if you want to redirect any http request to https, add a global middleware:

class ForceHttpsMiddleware{    public function handle($request, Closure $next){        if (!$request->secure() && App::environment() === 'production') {//optionally disable for localhost development            return redirect()->secure($request->getRequestUri());        }        return $next($request);    }}


I got everything to work by enforcing SSL on Couldflare. It's not in the code but it works.