Implementasi SSL laravel 5 , Redirect to https di shared hosting

20 Juli 2019

Laravel Laravel Trik

Dijaman teknologi website sekarang ini, hampir dipastikan rata-rata orang sudah menggunakan layanan SSL (Let’s Encrypt) pada domain websitenya, banyak manfaat yg di dapat jika domain sudah SSL, selain karena masalah keamanan, tentunya proses SEO untuk website bisa terbantukan, karena google sekarang menilai website harus https, kalo tidak maka akan terkalahkan ratingnya di mata google, google akan melirik dulu website yg sudah secure.

 

Mari kita mulai Implementasi SSL laravel 5 :

Prakteknya saya menggunakan : Laravel 5.6

 

Pertama

Buat file baru dengan nama HttpsProtocol.php ( app\Http\Middleware\HttpsProtocol.php)

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;

class HttpsProtocol
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->secure() && App::environment() === 'production') {
                return redirect()->secure($request->getRequestUri());
            }

        return $next($request);
    }
}

 

Kedua

buka file Kernel.php ( app\Http\Kernel.php)

tambahkan script berikut :

\App\Http\Middleware\HttpsProtocol::class,

sehingga hasilnya seperti ini :

<?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 = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            
            // ini kode yg baru kita tambahkan
            \App\Http\Middleware\HttpsProtocol::class,
        ],

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

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
....

 

Semoga berhasil, silahkan di load alamat website,

selamat mencoba, semoga bermanfaat.

 

sumber :

stackoverflow

Implementasi SSL laravel 5 , Redirect to https di shared hosting
5 (100%) 2 votes

Silahkan tulis komentar anda !

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

img
Laravel