0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel の TrustedProxy な環境でhttpへのリダイレクトを起こさないようにする

Posted at

想定状況

開発時はhttpで 本番環境だけhttpsなURLで実装したりするときに踏みそうな話。

TrustedProxyでLaravelにhttpsと認識させたとしても、Apache側は認識してくれないので、httpへのリダイレクトが帰ってくる場合があります。
一番ありがちなのは https://laravel/aaa/bbb/ccc/ から https://laravel/aaa/bbb/ccc へのリダイレクトとか。

ただのリンク遷移なら見た目問題ないものの、Ajax経由でAPIを叩いてると通信エラー吐くので要注意。

修正内容

とりあえず末尾 / を外すリダイレクトはhttps固定にしておく。

diff --git a/public/.htaccess b/public/.htaccess
index 9aec9e17..91bd9143 100644
--- a/public/.htaccess
+++ b/public/.htaccess
@@ -12,7 +12,7 @@
     # Redirect Trailing Slashes If Not A Folder...
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} (.+)/$
-    RewriteRule ^ %1 [L,R=301]
+    RewriteRule ^ https://%{HTTP_HOST}%1 [L,R=301]

     # Send Requests To Front Controller...
     RewriteCond %{REQUEST_FILENAME} !-d
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?