LoginSignup
6
4

More than 3 years have passed since last update.

NGINX+HTTPS(SSL)+PHPのサーバーでのiOS(WebKit)におけるロケーションヘッダーの挙動メモ

Last updated at Posted at 2016-05-12

自鯖でhttps(ssl)化してphpでロケーションヘッダを使った時に思わぬ挙動をしたのでメモ

環境

  • DTI ServersMan@VPS Entryプラン
  • Ubuntu 14.04.3 LTS
  • PHP 5.5.9-1ubuntu4.14
  • NGINX 1.9.10
  • iOS 9.3.1

PHPファイル

redirect.php
<?php
  // $urlに代入
  $url = 'http://example.com/'

  // リダイレクト
  header('Location: ' . $url);

実行結果

http://example.com/,http://example.com/

というようになぜかカンマ(,)を挟んで2回目繰り返されたリンクへリダイレクトしてしまいます
HTTPSにしているときだけこうなるのでiOSのバグなのかわかりませんが動かないものは仕方がないという感じでとりあえず

redirect.php
 <?php
   //...

   // リダイレクト
-  header('Location: ' . $url);
+  header('refresh:1;url=' . $url);

という形にしています

追記

追記1

リフレッシュの書き方が間違っていたので修正

-header('refresh:1,url=' . $url);
+header('refresh:1;url=' . $url);

追記2

Windows10の内蔵ブラウザであるMicrosoft EdgeでリフレッシュヘッダーだとPOSTが残ってしまうのでそれの対処として

functions.php
<?php
  //...

  // iOSのみrefreshヘッダーにする
  public function http_refresh($url) {
    $ua = $_SERVER['HTTP_USER_AGENT'];
    if(strpos($ua,'iPhone') || strpos($ua,'iPod') || strpos($ua,'iPad')) {
      $browser = 'ios';
    }
    if($browser === 'ios'){
      header('refresh:1;url=' . $url);
    }else{
      header('Location: ' . $url);
    }
  }

を追加して

redirect.php
  <?php
   //...

+  // functions.phpの読み込み
+  include(dirname(__file__) . '/functions.php');

   //$urlへリダイレクト
-  header('refresh:1;url=' . $url);
+  http_refresh($url);

とすることでLocationヘッダーが動く環境で使えるようにしています

6
4
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
6
4