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 Import Export Excel & CSV File In Laravel 10
How To Import Export Excel & C...

In this article, we will see how to import and export Excel & CSV files in laravel 10. Here, we will learn about lar...

Read More

Mar-08-2023

How to Add Bootstrap 5 in Angular 17 Application
How to Add Bootstrap 5 in Angu...

Welcome to this tutorial where I'll guide you through the process of integrating Bootstrap 5 into your Angular 17 ap...

Read More

Mar-25-2024

Date Range Filter In Datatable jQuery Example
Date Range Filter In Datatable...

In this article, we will see the date range filter in the datatable jquery example. Many times we required data of ...

Read More

Nov-14-2022

jQuery Show and Hide Effects Example
jQuery Show and Hide Effects E...

Hello friends, in this tutorial, we will see jQuery show and hide effects example. jQuery show method and jQuery...

Read More

Jan-21-2022