It is a struggle to get relative URLs to work properly with Laravel routes and helpers, such as {{ url('/') }} when your website is routed through Cloudflare or a Reverse Proxy SSL. No matter what you do it will always return http instead of https. This can cause insecure content and functionality errors.

Laravel relies on Symfony's request class to detect if an SSL certificate is setup on your server. It is obviously unable to detect this if you are routing traffic behind a reverse proxy.

A quick trick is to force https schema in your AppServiceProvider Boot Method.

In Laravel 5.3: app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL; //add URL Facade

class AppServiceProvider extends ServiceProvider
{
      /**
      * Bootstrap any application services.
      *
      * @return void
      */
      public function boot()
      {
      //
      URL::forceSchema("https"); //force HTTPS schema
      }
}

Updated 09-20-2017

In newer versions of Laravel the forceSchema has changed to forceScheme as illustrated below:

In Laravel 5.4+: app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
      /**
      * Bootstrap any application services.
      *
      * @return void
      */
      public function boot()
      {
      //
      \URL::forceScheme('https'); //force HTTPS schema
      }
}

The above snippet will quick and swiftly enforce https schema throughout your routes and views

It is also highly recommended that you add your reverse proxy to a trusted proxy header listed. You can find more details here.

Code Snippet By

Mike Biegalski


Biegalski LLC

Bio

A professional full stack developer, having experience in all aspects of the development cycle. Actively diving into new technology just to experience the workflow and see how it functions.

Submit An Article

We are open to accepting articles written by members of the community. If you have a quality article that you would like featured please, submit the article here.