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.
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 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;
}
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');
You can set 'url' => 'https://youDomain.com' in config/app.php
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]
In your .env file, just use
FORCE_HTTPS=true
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 = '*';
A list of the best spas in Dubai that will relax your body and mind…
Canada is a majestically beautiful country full of natural wonders and is also home to…
Here is a list of the Best USA Makeup Brands in 2023, that are safe…
Here is a list of the Best Australian Makeup Brands in 2023, that are safe…
Content creators always try hard to make their content reach a large number of followers…
Truck companies are constantly searching for people to fulfill their logistics, transportation, and supply chain…