3 Ways to Force Redirect Laravel Application from http to https

3 Ways to Force Redirect Laravel Application from http to https

If you are facing problem with redirecting laravel application from http to https then you are in the right place. In this article i am going to explain you how you can redirect laravel application http to https using three working methods.

Method 1: Using .htaccess

Simply add these two lines in your laravel public directory .htaccess file

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

After adding this two lines the .htaccess file will look something like this

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Method 2: Using Service Provider

You can also redirect from http to https by adding \URL::forceScheme('https');  in your AppServiceProvider.php boot function

<?php

namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \URL::forceScheme('https');
        Paginator::useBootstrap();
    }
}

 

Method 3: Using Middleware

To redirect using middleware at first create a middleware using the below command

php artisan make:middleware ForceSSL

Add the below codes to the middleware 

<?php

    namespace App\Http\Middleware;
    use Closure;
    class ForceSSL{    
         /**     * Handle an incoming request.     *     
        * @param  \Illuminate\Http\Request $request     
        * @param  \Closure $next     
        * @return mixed     */    

        public function handle($request, Closure $next)    {        
            if (!$request->secure() && in_array(env('APP_ENV'), ['stage', 'production'])) {            
                    return redirect()->secure($request->getRequestUri());       
            }        
            return $next($request);    
         }
}

No register the middleware in the karnel.php 

\App\Http\Middleware\ForceSSL::class,

Now this middleware will redirect to https when the application is in production mode. To change the mode from local to production edit the .env file and change

APP_ENV=production

 

 

 

Join Our Newsletter Now

Get E-mail updates about us.