Hello friends today we will learn how to force redirect Laravel app http protocol to https protocol. We will give you example how to force redirect protocol using .htaccess file in hosting server, so that you can apply in your hosting Laravel project.
We will show you with three options to redirect you site to secure protocol (https). We will write module in .htaccess file and other option using middleware in Laravel. Let's see how to redirect http to https, force Laravel redirect HTTP to HTTPs, Laravel redirect HTTP to HTTPs, using middleware force redirects to HTTPs, how to force redirects protocol HTTP to HTTPs in Laravel 7, Laravel 8 Laravel 9, how to redirect HTTP to HTTPS in laravel, laravel disable HTTPS redirect.
HTTPS stands for Hyper Text Transfer Protocol Secure which is advanced and secure version of HTTP. It supports encrypted and secure identification of a network server. This is help you to create secure connection between server and client which sent data over internet are encrypted.
To redirect http to https via .htaccess file, you have to rewrite the following script below
Open file root project -> .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>
Another option we can use service provider to force redirect http to https.
Open file app -> Providers -> AppServiceProvider.php
<?php
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()
{
if($this->app->environment('production'))
{
\URL::forceScheme('https');
}
}
}
All incoming requests in Laravel are goes through the middleware so, that mean we can redirect protocol from HTTP to HTTPS by checking the incoming request is secure or not.
Open terminal or command prompt type following command
php artisan make:middleware HttpsMiddleware
Open file app -> Http -> Middleware -> HttpsMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class HttpsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
// check if environment is production, you have to modified in .env file
if(env('APP_ENV') === "production")
{
if (!$request->secure())
{
return redirect()->secure($request->path());
}
}
return $next($request);
}
}
REMEMBER : whenever you modified .env file you are required to clear the configuration cache of the application by run following command
php artisan config:clear
After we create middleware we have to register in kernel.php file and the kernel will filter out all coming request through middleware.
Open file app -> Http -> Kernel.php
protected $middleware = [
....
\App\Http\Middleware\HttpsMiddleware::class,
];
I hope this short article help you to deploy correct protocol. Have a nice day thanks
You might Also Like:
Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him