Join WhatsApp ChannelJoin Now

Laravel HTTP to HTTPS Redirect Example

Hi Dev,

Today, i we will show you laravel HTTP to HTTPS redirect example. This article will give you simple example of laravel HTTP to HTTPS redirect example. you will learn laravel HTTP to HTTPS redirect example. So let’s follow few step to create laravel HTTP to HTTPS redirect example.

For this I will give you two examples, one from htaccess file and the other from laravel middleware. this Example you can use with laravel 6, laravel 7 and laravel 8 version as well.

So let’s follow few step to create laravel HTTP to HTTPS redirect example.

Laravel 8 Force Redirect HTTP to HTTPS using htaccess

.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 8 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();
    }
}

Recommended Posts