HTTP redirect to HTTPS with LARAVEL

Asked

Viewed 1,927 times

1

Good afternoon devs, I’m having trouble rerouting HTTP to HTTPS, I have the following content in my . HTACCESS

# Redireciona para o HTTPS independente do domínio
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}%public/ [R=301,L]

# Adiciona www. no prefixo do domínio
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteEngine On 
RewriteRule ^(.*)$ public/$1 [L] 

when accessing the site via https://url it works but when I type only www.dominio.com.br the following url appears

http://www.dominio.com.br/public/https://www.dominio.com.br

I need a force to fix this. Thank you

1 answer

2


In Laravel a middleware to force the redirect as follows:

Create in the folder app\Http\Middleware a file named after RedirectHttps.php and in its contents:

<?php namespace App\Http\Middleware;

use Closure;

class RedirectHttps
{   
    public function handle($request, Closure $next)
    {
        if (!$request->secure() && env('APP_ENV') === 'dev') 
        {
            return redirect()->secure($request->getRequestUri());
        }
        return $next($request);
    }
}

Remembering that this process can be done by the command line

php artisan make:middleware RedirectHttps

After creating the file needs to be recorded it in the file kernel.php contained in the folder app\Http\ adding a row as follows in the configuration named $middlewareGroups:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\RedirectHttps::class,     
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

This configuration will work very well on the production server as it is in the code informed (env('APP_ENV') === 'dev').

If your version of Laravel is greater than or equal to 5.4 (>=5.4) has a method to be invoked in App\Providers\AppServiceProvicer.php with the following code URL::forceScheme('https');, example:

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

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

that helps in the generation of urls of the site, but, does not redirect equal to the first configuration, ie, essential is the first part of the answer. In the file .htaccess can be make a configuration too

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTPS} !=on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

but, the redirect only worked using the middlweare servant.

  • The second option worked for me. I don’t understand the risks but I’ll leave this option to force https into the Portable for another day. Thank you very much and I will leave a "Plus,Like" marked in your reply. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.