Site Admin
Site Admin Founder of MeaningArticles
352 Views

Laravel force redirect HTTP to HTTPS

Hello Dev.

This articles is focused on laravel force redirect to https. this articles will provide you simple example of force redirect to https htaccess laravel. i would love to proportion with you laravel force https provider. you'll analyze laravel force https htaccess.

Right here, i can give you two methods to force redirect http to https your website all urls. one the usage of htaccess file and every other the usage of laravel middleware. you may comply with this academic and you can use with laravel 6, laravel 7 and laravel 8 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'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.