简体   繁体   English

Yii URL管理HTTPS

[英]Yii URL Management HTTPS

Im using a code to separate pages that is HTTPS and HTTP in my website 我使用代码来分隔我网站中的HTTPS和HTTP页面

The problem is: When Im on HTTP, links to HTTPS no have WWW and vice versa. 问题是:当我在HTTP上时,指向HTTPS的链接没有WWW,反之亦然。 I did not find the problem in the script. 我没有在脚本中找到问题。

public function createUrl($route, $params = array(), $ampersand = '&')
{
    $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->isSecureRoute($route);
    if (Yii::app()->request->isSecureConnection) {
        return $secureRoute ? $url : 'http://' . Yii::app()->request->serverName . $url;
    } else {
        return $secureRoute ? 'https://' . Yii::app()->request->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->isSecureRoute($route);
    $sslRequest = $request->isSecureConnection;
    if ($secureRoute !== $sslRequest) {
        $hostInfo = $secureRoute ? 'https://' . Yii::app()->request->serverName : 'http://' . Yii::app()->request->serverName;
        if ((strpos($hostInfo, 'https') === 0) xor $sslRequest) {
            $request->redirect($hostInfo . $request->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->_secureMap === null) {
        foreach ($this->secureRoutes as $r) {
            $this->_secureMap[strtolower($r)] = true;
        }
    }
    $route = strtolower($route);
    if (isset($this->_secureMap[$route])) {
        return true;
    } else {
        return ($pos = strpos($route, '/')) !== false 
            && isset($this->_secureMap[substr($route, 0, $pos)]);
    }
}

} }

Code adapted from: http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/ 代码改编自: http//www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/

It's better to manage this at the controller level using filters. 最好使用过滤器在控制器级别进行管理。

In your components directory setup 2 filters HttpsFilter and HttpFilter as follows:- 在您的组件目录设置2中按如下方式过滤HttpsFilterHttpFilter : -

class HttpsFilter extends CFilter {

    protected function preFilter( $filterChain ) {
        if ( !Yii::app()->getRequest()->isSecureConnection ) {
            # Redirect to the secure version of the page.
            $url = 'https://' .
                Yii::app()->getRequest()->serverName .
                Yii::app()->getRequest()->requestUri;
                Yii::app()->request->redirect($url);
            return false;
        }
        return true;
    }

}

and

class HttpFilter extends CFilter {

    protected function preFilter( $filterChain ) {
        if ( Yii::app()->getRequest()->isSecureConnection ) {
            # Redirect to the secure version of the page.
                $url = 'http://' .
                Yii::app()->getRequest()->serverName .
                Yii::app()->getRequest()->requestUri;
                Yii::app()->request->redirect($url);
            return false;
        }
        return true;
    } 
}

then in each controller force https using the filters, optionally by action: 然后在每个控制器中使用过滤器强制https,可选择通过操作:

class SiteController extends Controller {

    public function filters()
    {
        return array(
            'https +index', // Force https, but only on login page
        );
    }
}

Edit: if the filters() function above doesn't seem to work for you, instead try 编辑:如果上面的filters()函数似乎不适合你,请尝试

return array(
           array('HttpsFilter +index'), // Force https, but only on login page
       );

See http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter (and comments on it). 请参阅http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter (及其评论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM