0

My site is saying 'not secure', I have added Https middleware but i don't understand how will i call it in the controller and i have tried forcesheme in Appserviceprovider but nothing seems to work, i have set the SSL in my domain. Please help.

My HttpsProtocol middleware

<?php
namespace App\Http\Middleware;
use Closure;
class HttpsProtocol {
public function handle($request, Closure $next)
{
  if (!$request->secure() && env('APP_ENV') === 'prod') {
    return redirect()->secure($request->getRequestUri());
  }
  return $next($request); 
}

Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        // appending custom middleware
        'MyApp\Http\Middleware\HttpsProtocol'

    ];

}
  • 2
    this must be done in the server settings(Apache, Nginx), not in the code – Kongulov Jul 15 '20 at 10:05
  • Possible duplicate of https://stackoverflow.com/questions/44113859/how-to-implement-https-in-laravel-5-4 – Stef Jul 15 '20 at 10:05
  • @RamizKongulov So i don't need to add any extra code for HTTPS redirect in my project file? Thanks a lot for the response though – Shyamal Jul 15 '20 at 12:15

2 Answers2

0

try this way in your htaccess file

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://YOURDOMAIN.COM/$1 [R,L]
0
namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https');
    }
}
NC64
  • 369
  • 5
  • 12
  • I did it but without adding URL facades and did not work. Let's see if it works...hope it works, Thanks a lot for the response though. – Shyamal Jul 15 '20 at 12:21