Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel Redirect from Http to Https Secure URL

Laravel Redirect from Http to Https Secure URL

Aman Jain, August 26, 2022March 16, 2024

To redirect from http to https secure url in laravel we can achieve by multiple ways. While deploying application to server we need to make our website secure and robust so attackers can not attack easily on website. In this tutorial we will learn to redirect the http to https secure url in laravel and apache too.

We will check multiple methods to redirect and all methods will work in all versions of laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.

Method 1 : Redirect to https using middleware

First method is using the laravel itself by creating the custom middleware and then in handle method we will check request is secure or not. if we are developing the application on localhost then we do not want this behavior to redirect the http url to https so we will also check the app environment is local or production so here i checked the production. You can change it according to your requirement.

namespace MyApp\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;

class HttpsRedirect {

    public function handle($request, Closure $next)
    {
            if (!$request->secure() && \App::environment() === 'production') {
                return redirect()->secure($request->getRequestUri());
            }

            return $next($request); 
    }
}

And add it in kernel.php web middleware so we can redirect every request of web application to https as follow

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,

        // here
        \App\Http\Middleware\HttpsRedirect::class

    ],
];

Or you can add it to specific route group or url in web.php file.

Method 2: Redirect using htaccess file

In this method we will use htaccess to redirect from http to https so this will work out of the box

RewriteEngine On

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

Here we simply enabled the RewriteEngine then checked if the request is not https then using the RewriteRule we are redirecting the request to https url.

Method 3 : Using forceScheme Method of URL Class to Redirect to Https

\URL::forceScheme('https'); is usefull while we are behind the proxy or we want to serve all interal assets urls to https then we need to resgister forceScheme in AppServiceProvider.php so it will redirect all application request to https

\URL::forceScheme('https');

and use it as below

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    
    public function register()
    {
       \URL::forceScheme('https');
    }
    public function boot()
    {
        //
    }
}

Related

Php Laravel Laravel 9 htaccesshttphttpslaravel

Post navigation

Previous post
Next post

Related Posts

Php How to use left join in Laravel Eloquent

How to use left join in Laravel eloquent query with example

February 5, 2022February 8, 2024

In this article we will learn to use left join in laravel eloquent and query builder. Laravel itself provide inbuilt method to left join the table like in MySQL or SQL based database we can do join multiple tables using leftJoin function. For example if you have 10 rows and…

Read More
Php How to Search JSON Data in Database Laravel

How to Search JSON Data in Database Laravel ?

August 2, 2022March 16, 2024

JSON Data in database is used to store the informational data. JSON can be easily parse and stringify so it can be stored easily in database. In this post i will show you to search JSON data in database laravel. Laravel by default configured with MySQL and we will store…

Read More
Php How to Call a POST Rest Api in Laravel

How to Call a POST Rest Api in Laravel ?

June 7, 2022June 7, 2022

In this article we will learn to call an post rest api in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. Post…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version