Force Redirect Website URL http to https in Laravel8

1784 views 1 year ago Laravel

In this article, I will share with you how to make force redirect website URL HTTP to HTTPS in laravel. as you mark, sometimes we install SSL in our live laravel website and still not auto redirect it HTTPS. so, here I have seen to you how to resolve this issue with some simple steps.

Example - 1 :

open your .htaccess file and add the following 2 line code in your file.

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Example - 2 :

open your app/Providers/AppServiceProvider.php file and make the following change. please don't use this option in your local. because after you make this change your laravel application not run on the local server. this option only works in live.

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
    	// force redirect https.
        \URL::forceScheme('https');
    }
}

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]