Force https on laravel application

754 0 0 0

Last Updated : 2024-04-25 10:47:33

If you have purchased a SSl certificate for your website and then you website is still serving the assets from an HTTP protocol instead of https, and also the form and redirects are going to http instead of https, then here is the way to fix this from within laravel AppServiceProvide.


  1. Change .env APP_ENV = production Instead of local. [This will help our application differentiate between local and online version, because we want to enforce https protocol only on online version].

  2. Add the following code to the boot method in the app\Providers\AppServiceProvider:
       public function boot()
    {
    //Schema::defaultStringLength(191); //fix the error for migration of max length of field
    //Force https on production servers
    if(env('APP_ENV') !== 'local') {
    URL::forceScheme('https');
    }
    }


  3. Now all is set :)

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .