How to Force Redirect Http to Https in Laravel

Websolutionstuff | Aug-11-2021 | Categories : Laravel

In this small artical we will see how to force redirect http to https in laravel, Here i will show you two method in laravel redirect to https first is laravel redirect HTTP to HTTPS via htaccess and second is laravel force redirect https using middleware.

So. let's see how to force redirect http to https in laravel.

 

Example 1 : Using .htaccess

 Redirect HTTP to HTTPS via htaccess. here you need to add code in your public/.htaccess file.

<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>

 

Example 2 : Using ServiceProvider

In this example you need to add code in this file 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();
    }
}

 


 

You may also like :

Recommended Post
Featured Post
How to Run Specific Seeder in Laravel 8
How to Run Specific Seeder in...

In this example, we will learn how to run a specific seeder in laravel 8. If you want to run only one seeder in laravel...

Read More

Jan-19-2022

7+ Laravel Tips: Optimize Database Queries (2024)
7+ Laravel Tips: Optimize Data...

Hey there, fellow developers! If you've been navigating the intricate world of Laravel, you know that optimizing dat...

Read More

Jan-03-2024

Laravel 10 AJAX CRUD Operations Example
Laravel 10 AJAX CRUD Operation...

In this article, we will see the laravel 10 ajax crud operations example. Here, we will learn about ajax crud operation...

Read More

Feb-27-2023

How To Install php-bcmath In Ubuntu
How To Install php-bcmath In U...

In this article, I will guide you through the process of installing php-bcmath on Ubuntu. Php-bcmath is a PHP extension...

Read More

Jul-12-2023