After the development of any application, it needs to be hosted on the server. After uploading and configuration of the application, you will be able to access your website through a URL. You also need an SSL certificate to secure the domain URL. But, after installing the SSL certificate the site doesn’t redirect from HTTP to HTTPS. It will allow you to open the website URL through HTTPS. But, you have to put HTTPS before the URL manually. Without having SSL the URL will show not secure in the browser. Generally, in the web application, you can redirect from HTTP to HTTPS using the .htaccess file. But sometimes it doesn’t work properly. Today, in this post, I will show you how you can redirect forcibly from HTTP to HTTPS in Laravel 8 application using middleware.
So, let’s begin by creating a new Laravel application here.
Prerequisites
For creating a new application in Laravel 8, you will require to have the below configuration.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- Composer
Here, I will start with the composer for creating the application.
Create REST API in Laravel 8 Using JWT Authentication
Create New Project in Laravel 8
Open the terminal and hit the below command for creating a new project.
composer create-project --prefer-dist laravel/laravel blog
The above command will start installing the Laravel 8 project.
After creating the project, just open it to the VS code editor.
Also, you may notice the URL currently, it is localhost:8001.
Now, let’s create a middleware first.
How to Create Github Login in Laravel 8 Using Socialite
Create Middleware in Laravel 8
In Laravel, all the incoming request goes through the middleware. You can check the type of requests and set the specific condition on behalf of that request. So, for redirection from HTTP to HTTPS, we can use middleware. This middleware will check the incoming request is secure or not.
So, let’s create a middleware first then I will write the condition.
In the terminal, hit the below command.
php artisan make:middleware HttpsMiddleware
It will create a file inside the app/Http/Middleware folder.
After creating the middleware, let’s add the condition to check the incoming requests.
<?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)
{
if (!$request->secure()) {
return redirect()->secure($request->path());
}
return $next($request);
}
}
- In the above snippet, I have created a condition to check if the incoming request is not secure. That means if URL has not HTTPS then it will redirect the request to secure path.
Now, move to the next step for registering this middleware.
How to Create Login with Twitter in Laravel 8 Using Socialite
Register Middleware in Laravel
The middleware needs to be registered inside the kernel.php The kernel filter out the incoming requests through the middleware by making a separation. So, inside the app/Http folder, you will see the kernel.php file. Just open it and register the created middleware.
protected $middleware = [
....
....
....
....
\App\Http\Middleware\HttpsMiddleware::class,
];
Create LinkedIn Login in Laravel 8 Using Socialite
Check the Result of HTTPS Redirection
Now, you can check the result in the browser by just refreshing the URL. You may notice the URL has been redirected from HTTP to HTTPS.
Also, your site will not be running with HTTPS in the local environment. This is because we don’t have the SSL in the local environment.
Here, the URL has been redirected forcibly from HTTP to HTTPS. But here is an issue, if you are in the local environment then it won’t allow you to run the Laravel application.
So, let’s fix it by separating it for local and production environment.
Create Socialite Login with Google Account in Laravel 8
Redirect HTTPS to Only Production Environment
Now, reopen the HttpsMiddleware.php file and add one more condition to check the app environment.
<?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
if(env('APP_ENV') === "production") {
if (!$request->secure()) {
return redirect()->secure($request->path());
}
}
return $next($request);
}
}
Here, it will check if the app environment is production then it will redirect all incoming requests to HTTPS.
Refresh the browser and you will see the application is running again.
But here you will have a doubt that is where you will set the environment?
How to Create Facebook Login in Laravel 8 Using Socialite
Set the Environment in Laravel Application
In order to set the application environment, you can navigate to the .env file of the Laravel project. You will find the APP_ENV there. By default, it will be local as showing below.
But, when you will upload the application to the server then you will have to set it to production.
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:a4MUa59tYPPg/XWQ7BS7Ny0M0CJUGUTQ1ar4Rrh8084=
APP_DEBUG=false
APP_URL=http://localhost
After changing the environment, you will require to clear the configuration cache of the application.
php artisan config:clear
The above command will clear the configuration cache of the previous environment. So, you will require a production environment when only you are uploading the website to a server. Rest if you are in the development phase then it is fine with the local environment.
How to Schedule Tasks with Cron Job in Laravel 8
So, that’s it for this post for redirecting to HTTPS.
Final Words
We have set the redirection from HTTP to HTTPS using the middleware in Laravel 8. This is a good approach at the application level to redirect forcibly. You can also redirect HTTP to HTTPS using the .htaccess file. But to redirect from HTTP to HTTPS using the .htaccess file you will need the redirection rule. If you configured Cloudflare then also you can use the Cloudflare redirection. So, I hope this post will help you somewhere when you will host your Laravel application to the server.
Leave a Reply