title: Yii URL Management HTTPS
body: <p>Im using a code to separate pages that is HTTPS and HTTP in my website</p>
  
  <p>The problem is: When Im on HTTP, links to HTTPS no have WWW and vice versa.
  I did not find the problem in the script.</p>
  
  <p>
  
  <pre><code>public function createUrl($route, $params = array(), $ampersand = '&amp;')
  {
      $url = parent::createUrl($route, $params, $ampersand);
  
      // If already an absolute URL, return it directly
      if (strpos($url, 'http') === 0) {
          return $url;  
      }
  
      // Check if the current protocol matches the expected protocol of the route
      // If not, prefix the generated URL with the correct host info.
      $secureRoute = $this-&gt;isSecureRoute($route);
      if (Yii::app()-&gt;request-&gt;isSecureConnection) {
          return $secureRoute ? $url : 'http://' . Yii::app()-&gt;request-&gt;serverName . $url;
      } else {
          return $secureRoute ? 'https://' . Yii::app()-&gt;request-&gt;serverName . $url : $url;
      }
  }
  
  public function parseUrl($request)
  {
      $route = parent::parseUrl($request);
  
      // Perform a 301 redirection if the current protocol 
      // does not match the expected protocol
      $secureRoute = $this-&gt;isSecureRoute($route);
      $sslRequest = $request-&gt;isSecureConnection;
      if ($secureRoute !== $sslRequest) {
          $hostInfo = $secureRoute ? 'https://' . Yii::app()-&gt;request-&gt;serverName : 'http://' . Yii::app()-&gt;request-&gt;serverName;
          if ((strpos($hostInfo, 'https') === 0) xor $sslRequest) {
              $request-&gt;redirect($hostInfo . $request-&gt;url, true, 301);
          }
      }
      return $route;
  }
  
  private $_secureMap;
  
  /**
   * @param string the URL route to be checked
   * @return boolean if the give route should be serviced in SSL mode
   */
  protected function isSecureRoute($route)
  {
      if ($this-&gt;_secureMap === null) {
          foreach ($this-&gt;secureRoutes as $r) {
              $this-&gt;_secureMap[strtolower($r)] = true;
          }
      }
      $route = strtolower($route);
      if (isset($this-&gt;_secureMap[$route])) {
          return true;
      } else {
          return ($pos = strpos($route, '/')) !== false 
              &amp;&amp; isset($this-&gt;_secureMap[substr($route, 0, $pos)]);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Code adapted from: <a href="http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/" rel="nofollow">http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/</a></p>