2017-02-07 7 views
1

私はHTTPSプロトコルを使用しているウェブサイト全体を持っています。しかし、最近私はYiiの非常に奇妙なbahaviourに気づいた。私はリダイレクト作るとき:Yii:奇妙なリダイレクトの振る舞い(https - > http)

HTTP://site.xyz/article/123456 

X-Poweredのバイ:

$this->redirect('/article/123456'); 

をそれは、最初のページのHTTPバージョン(私は、ヘッダーで見ることができます)に302リダイレクトありませんPHP/5.4.45-0 + deb7u2

そしてnginxのは、HTTPSバージョンに301リダイレクトん

HTTPS://site.xyz/article/123456 

どうして、Yiiは絶対URLを構築するのですか(常に相対的URLを使用すると考えられます)

答えて

1
/** 
    * Redirects the browser to the specified URL. 
    * @param string $url URL to be redirected to. Note that when URL is not 
    * absolute (not starting with "/") it will be relative to current request URL. 
    * @param boolean $terminate whether to terminate the current application 
    * @param integer $statusCode the HTTP status code. Defaults to 302. See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html} 
    * for details about HTTP status code. 
    */ 
public function redirect($url,$terminate=true,$statusCode=302) 
    { 
     if(strpos($url,'/')===0 && strpos($url,'//')!==0) 
      $url=$this->getHostInfo().$url; 
     header('Location: '.$url, true, $statusCode); 
     if($terminate) 
      Yii::app()->end(); 
    } 

Source

URLパラメータに関する注意事項を確認してください:にリダイレクトする

@paramの文字列$ URLのURLを。 URLが *アブソリュート(「/」で始まらない)でない場合、現在のリクエストURLからの相対的なものになります。

しかし、あなたのコードでURLが「/」で始まっているので、ソースコードによると、どうなる次のことは、次のとおりです。$のURL =の$ this - >はGetHostInfo()$のURL。

それでははGetHostInfo機能のソースを見てみましょう:

public function getHostInfo($schema='') 
    { 
     if($this->_hostInfo===null) 
     { 
      if($secure=$this->getIsSecureConnection()) 
       $http='https'; 
      else 
       $http='http'; 
      if(isset($_SERVER['HTTP_HOST'])) 
       $this->_hostInfo=$http.'://'.$_SERVER['HTTP_HOST']; 
      else 
      { 
       $this->_hostInfo=$http.'://'.$_SERVER['SERVER_NAME']; 
       $port=$secure ? $this->getSecurePort() : $this->getPort(); 
       if(($port!==80 && !$secure) || ($port!==443 && $secure)) 
        $this->_hostInfo.=':'.$port; 
      } 
     } 
     if($schema!=='') 
     { 
      $secure=$this->getIsSecureConnection(); 
      if($secure && $schema==='https' || !$secure && $schema==='http') 
       return $this->_hostInfo; 
      $port=$schema==='https' ? $this->getSecurePort() : $this->getPort(); 
      if($port!==80 && $schema==='http' || $port!==443 && $schema==='https') 
       $port=':'.$port; 
      else 
       $port=''; 
      $pos=strpos($this->_hostInfo,':'); 
      return $schema.substr($this->_hostInfo,$pos,strcspn($this->_hostInfo,':',$pos+1)+1).$port; 
     } 
     else 
      return $this->_hostInfo; 
    } 

私は_hostInfoがあったこと、それはおそらく見つけることはありませんしながら、私は最初の条件がtrueを返した場合(チェックするためにあなたをお勧めします$ this-> getIsSecureConnection()関数の結果をチェックすることができます。

+0

ありがとう、私は問題を発見した。サーバーが$ _SERVER ['HTTPS'](https://github.com/yiisoft/yii/blob/1.1.17/framework/web/CHttpRequest.php#L581)を設定していないため、この現象が発生しています。回避策として、私は* index.php *で手動で設定していますが、より永続的な解決策を探しています。 –

+1

さて、これはもっとまれな状況ですが、次の手順を実行する場合は、getIsSecureConnection関数(https://github.com/yiisoft/yii/blob/04f97932ddfbf9c5bdaae0a6a41b851544704a27/framework/web/CHttpRequest)を確認することです。 .php#L581)結局、結局同じ結論に至りました。がんばろう。 –