All Posts

Laravel

How to force redirect HTTP to HTTPS in Laravel 9.x, 8.x, 7.x and 6.x

in this article you will learn to force Laravel project to use HTTPS for all links such as routes if you are facing an issue of not secure a URL, then don't worry about this, the best solution of How to force redirect HTTP to HTTPS in Laravel 8.x, 7.x and 6.x is shown in this article.

Last updated : Oct 10, 2021

Co-authored by : Nilmani

NO IMAGE AVAILABLE
Share (Copy this link):

HTTP vs. HTTPS: What are the differences?

HTTPS is HTTP with encryption. The only difference between the two protocols is that HTTPS uses TLS (SSL) to encrypt normal HTTP requests and responses. As a result, HTTPS is far more secure than HTTP. A website that uses HTTP has http:// in its URL, while a website that uses HTTPS has https://. The S in HTTPS stands for "secure."

http vs https

How do I redirect to https?

So all we want now is to make our website more secure. There are several methods of enabling an Apache redirect HTTP to HTTPS. In this article we will discuss about two methods:

  1. Redirect to https using .htaccess file
  2. Using laravel middleware
  3. Through AppServiceProvider.php file

1. How do we redirect http to https using .htaccess file?

Redirecting HTTP to HTTPS is easy with .htaccess file, but if we are using laravel framework then we can use middleware too. This method will redirect all web traffic to https. If you don't have an existing .htaccess file, then create it on root directory. ie. in public folder and add the following (Missing) code.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

2. How do I redirect http to https using middleware in Laravel?

In this method you have to create a middlware class (file), and register that middlewawre in kernel.php. So this middleware will handle our https redirection.

First Create Https Protocol Middleware

Let's begin by creating file in following directory with name "HttpsProtocol.php". And write the code provided below

path: app/Http/Middleware/HttpsProtocol.php

Or,

You can also create the same middleware file from artisan command.

php artisan make:middleware HttpsProtocol

After creating the middleware file wite the code provided below:

<?php
namespace App\Http\Middleware;

use Closure;

class HttpsProtocol {

	public function handle($request, Closure $next)
	{
			if (!$request->secure()) {
			return redirect()->secure($request->getRequestUri());
			}

			return $next($request); 
	}
}
?>

Next, we have to register this middleware in Kernel.php file

So open Kernel.php from the location: app/Http/Kernel.php

You just need to add this line in the middleware Groups section.

\App\Http\Middleware\HttpsProtocol::class,
protected $middlewareGroups = [
		'web' => [
			\App\Http\Middleware\EncryptCookies::class,
			.
			.
			.
			\Illuminate\Routing\Middleware\SubstituteBindings::class,
			\App\Http\Middleware\HttpsProtocol::class,
		],

 

3. Through App service provider

Place the following code in the boot() method of AppServiceProvider.php file.

if($this->app->environment('production')) {
	\URL::forceScheme('https');
}

 

Hope this solve your problem, if not then please comment below

More post by Nilmani

NO IMAGE AVAILABLE
Laravel

How to force redirect HTTP to HTTPS in Laravel 9.x, 8.x, 7.x and 6.x

in this article you will learn to force Laravel project to use HTTPS for all links such as routes if you are facing an issue of not secure a URL, then don't worry about this, the best solution of How to force redirect HTTP to HTTPS in Laravel 8.x, 7.x and 6.x is shown in this article.

Nilmani
2 years ago
NO IMAGE AVAILABLE
C#

Download and Install visual studio and preparing our IDE on windows 11

Microsoft Visual Studio is an integrated development environment from Microsoft. It is used to develop computer programs, as well as websites, web apps, web services and mobile apps, Games, etc.

Nilmani
2 years ago
NO IMAGE AVAILABLE
C#

Introduction to c#

C# is pronounced "C-Sharp". It is an object-oriented programming language created by Microsoft that runs on the .NET Framework. C# has roots from the C family, and the language is close to other popular languages like C++ and Java.

Nilmani
2 years ago
NO IMAGE AVAILABLE
Alpine Js

What is x-data in alpine and how to use

x-data defines a chunk of HTML as an Alpine component and provides the reactive data for that component to reference.

Nilmani
2 years ago
NO IMAGE AVAILABLE
Alpine Js

Installing and using Alpine on your website

Alpine.js is a refreshingly minimal JavaScript framework that gives you the reactive nature of Vue and React but with much more simplicity. In this article, we will learn to install alpine.js on our website.

Nilmani
2 years ago
0 Comments

Some HTML is ok.

Don't forget the topic.

No comments yet