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 Running raw sql query in migration laravel

How to run raw SQL query in migration Laravel ?

February 12, 2022February 15, 2022

Laravel provides its inbuilt feature to take care of migrations, Migrations like to make the version control for our database schema and share it across the developers but in laravel migrations we need to create eloquent query Using the schema class or Facade and then we need to run it…

Read More
Php How to Upload image with preview in Laravel 9 with example

How to Upload image with preview in Laravel 9 with example ?

May 14, 2022May 14, 2022

Upload image with preview in laravel or for any website can be basic requirement like set up a profile picture to providing the documents. Laravel 9 provides robust functionality to upload and process the image with security. Laravel simply use Request file method to access the uploaded file and then…

Read More
Laravel Laravel 10 Image Upload with Preview Example

Laravel 10 Image Upload: A Comprehensive Tutorial

October 23, 2023March 16, 2024

Laravel 10, renowned for its efficiency and simplicity, offers a seamless solution for handling image uploads in web applications. In Laravel 10 Image Upload tutorial, we will guide you through the process of uploading and displaying images using the robust features of Laravel 10. Whether you are a beginner or…

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

  • 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
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version