ItSolutionStuff.com

How to Force Redirect HTTP to HTTPS in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hi,

This example is focused on laravel force redirect to https. This article will give you simple example of force redirect to https htaccess laravel. i would like to share with you laravel force https provider. you will learn laravel force https htaccess.

Here, i will give you two ways to force redirect http to https your website all urls. one using htaccess file and another using laravel middleware. you can follow this tutorial and you can use with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version as well.

let's see both example:

Laravel - Force Redirect HTTP to HTTPS using htaccess

public/.htaccess

<IfModule mod_rewrite.c>

<IfModule mod_negotiation.c>

Options -MultiViews -Indexes

</IfModule>

RewriteEngine On

RewriteCond %{HTTPS} !=on

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Authorization Header

RewriteCond %{HTTP:Authorization} .

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (.+)/$

RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

</IfModule>

Laravel - Force Redirect HTTP to HTTPS using Provider

app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

}

/**

* Bootstrap any application services.

*

* @return void

*/

public function boot()

{

\URL::forceScheme('https');

Paginator::useBootstrap();

}

}

i hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Http Curl Delete Request Example

Read Now →

Laravel Get Next and Previous Record with URL Example

Read Now →

Laravel Convert PDF to Image Example

Read Now →

Laravel Summernote Image Upload Example

Read Now →

Laravel Image Upload with Spatie's Media Library Example

Read Now →

Laravel Google Autocomplete Address Example

Read Now →

Laravel Eloquent Select Single Column to Array Example

Read Now →

Laravel File Manager Tutorial Step by Step

Read Now →

Laravel 8 Install Bootstrap Example Tutorial

Read Now →

Laravel 8 Markdown | Laravel 8 Send Email using Markdown Example

Read Now →

Laravel 8 Multiple Database Connection Example

Read Now →