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!
– Emilio Dami Silva