LoginSignup
5
2

More than 5 years have passed since last update.

Herokuでhttpをhttpsにリダイレクトさせる。(フレームワークはLaravel)

Posted at

通常は.htaccessでリダイレクトさせるのが良いのですが、何故か上手く行かなくてフレームワーク側でリダイレクトさせました。

プログラム

Middlewareを新たに追加します。

app/Http/Middleware/HttpsProtocol.php

<?php
namespace App\Http\Middleware;

use Closure;

class HttpsProtocol {

    public function handle($request, Closure $next)
    {
        if (env('APP_ENV') === 'production') {
            $request->server->set('HTTPS', 'on');
            if ($request->header('x-forwarded-proto') <> 'https') {
                return redirect()->secure($request->getRequestUri());
            }
        }

        return $next($request);
    }
}

作ったMiddlewareを登録します。

app/Http/Kernel.php

    protected $middlewareGroups = [
        'web' => [
            \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,
            \App\Http\Middleware\HttpsProtocol::class, // ここに追加
        ],

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

こんな感じで行けるはずです。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2