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 Custom Error pages in laravel

How to create a custom 404 page in Laravel 8 ?

December 28, 2021December 28, 2021

Laravel provides its inbuilt 404 error page and shows text 404 page not found but we want more customization and better ui for 404 page so in this article i will show yo to customize the 404 not found error page in laravel. This article will cover you to create…

Read More
Php How to set timezone dynamically and globally in Laravel

How to set timezone dynamically and globally in Laravel ?

September 20, 2022March 16, 2024

Laravel stores timezone configurations in config/app.php file and we can easily set timezone dynamically and globally in Laravel. Laravel usage carbon library to get the current date time and format, carbon also respects the timezone defined in config/app.php file. After php 5, php also introduced DateTime class to conduct the…

Read More
Php upload file in php

How to upload file in php ?

September 13, 2021September 29, 2021

In this tutorial, we will learn basic of uploading of a file in php and also basic configuration required for php file upload. Configure the php.ini for large file upload By default php support 2 mb of file upload but in real world we require more than 2 mb. Same…

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

  • 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 Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version