Enable HTTPS in Laravel

11-12-2019

In laravel, we can enforce all routes to https by using following steps:

Open AppServiceProvider class and edit as follows:

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if(config('app.env') === 'prod'||config('app.env') === 'production') {
            URL::forceScheme('https');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

edit root .htacess file

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove public folder form URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Note: Don't forget to change APP_ENV variable as "prod"

© 2019 All rights reserved. Codesenior.COM