Articles

How to force HTTPS on Laravel for all routes? Using Different Methods to make URLs secure

In this article I’ll show how to force Laravel project to use HTTPS for all links such as routes, assets. Some of the codes are taken from scratchcoding Laravel Code Examples. Let’s start to find out different methods of how to force https on laravel:

Do not forget to clear Laravel cache after making below changes.

AppServiceProvider in the boot()

Place this in the AppServiceProvider in the boot() method

File Path: app->providers->AppServiceProvider.php

if($this->app->environment('production')) {
    \URL::forceScheme('https');
}

Configure your web server

Configure your web server to redirect all non-secure requests to https. Example of a nginx config:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

web.php or api.php

use the following line at the end of the web.php or api.php file and it worked perfectly:

File Path : routes->web.php / routes->api.php

URL::forceScheme('https');

config/app.php

You can set 'url' => 'https://youDomain.com' in config/app.php

.htaccess

Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.env

In your .env file, just use

FORCE_HTTPS=true

App\Http\Middleware\TrustProxies.php

For laravel 8, if you tried all of the above methods but got browser redirected you too many times error, please set proxies in TrustProxies middleware like the following:

/**
  * The trusted proxies for this application.
  *
  * @var array|string|null
*/protected $proxies = '*';
CADSLIST SEO BLOG

Recent Posts

The Best Spas in Dubai for the Ultimate Luxury Experience

A list of the best spas in Dubai that will relax your body and mind…

October 3, 2023

12 of The Best Make-Up Brands Available in Canada

Canada is a majestically beautiful country full of natural wonders and is also home to…

September 22, 2023

10 Best Makeup Brands in the United States

Here is a list of the Best USA Makeup Brands in 2023, that are safe…

September 20, 2023

The Best Australian Makeup Brands That You Need To Try

Here is a list of the Best Australian Makeup Brands in 2023, that are safe…

September 10, 2023

How to Create TikTok Content that can go Viral Easily?

Content creators always try hard to make their content reach a large number of followers…

September 8, 2023

Top Job Websites for Truck Drivers in Canada

Truck companies are constantly searching for people to fulfill their logistics, transportation, and supply chain…

August 11, 2023