How To Force Redirect HTTP To HTTPS In Laravel

In this article, we will see how to force redirect HTTP to HTTPS in laravel Here we will show you two methods in laravel redirect to HTTPS first is laravel redirect HTTP to HTTPS via htaccess and the second is laravel force redirect HTTPS using middleware.

So, let's see how to force redirect HTTP to HTTPS in laravel, laravel redirect to HTTPS, laravel redirect HTTP to HTTPS via htaccess, laravel force redirects HTTPS using middleware, how to redirect HTTP to HTTPS in laravel 6/7/8, laravel 6/7/8 change HTTP to HTTPS, how to redirect HTTP to HTTPS in laravel, laravel disable HTTPS redirect.

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 might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS