9
9

More than 5 years have passed since last update.

SSLターミネーション環境でもLaravelを動かしたい

Posted at

意外と無かったので書くよ。
開発中にわざわざ証明書買うのももったいないのでHTTPでテストしてて、いざHTTPSで動かそうとしたら動かないのはよくあること。
ELBとかでSSLターミネーションするような環境で例えばHTMLビルダーでURLを作ってる所がhttp://で展開されちゃうと、httpsでアクセスしてると外部ファイルの読み込みが上手く行かなかったりするわけです。
そんな時は

filters.php
App::after(function($request, $response)
{
    if (App::environment('production')){
        // Locationヘッダの書き換え
        $location = $response->headers->get('Location');
        if(strstr($location, 'http://')){
            $location = str_replace(url('/'), secure_url('/'),$location);
            $response->header('Location',$location,true);
        }
        // ソース内のURLスキームを全部書き換え
        $content = $response->getContent();
        $content = str_replace(url('/'), secure_url('/'), $content );
        $response->setContent($content);
});

こんな風にフィルタのApp::afterでLocationヘッダとかURLをゴリっとhttpsに書き換えてあげると動いたよ、っていうメモでした。

9
9
1

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
9
9