2010-12-17 121 views
1

我开发了一个cakephp站点,它应该对所有页面使用ssl。它按预期工作,除非在控制器中使用重定向时,它会重定向到http://subdomain.domain.com而不是https://subdomain.domain.com/controller/action。重定向到cakephp ssl站点路由到http而不是https

我已经通过创建端口80指向的CakePHP应用程序的虚拟主机解决了这个和添加在.htaccess

的RewriteCond%{HTTPS}这些重写规则关闭 重写规则(。*)https://% {HTTP_HOST}% {REQUEST_URI} [L]

这会捕获这种情况并重定向到https,但这会给服务器带来不必要的额外流量。

这个额外的流量的原因是重定向功能,因为它会产生错误的网址。我查看了重定向函数,它调用router :: url来创建实际的url。但我无法弄清楚如何或在何处指示路由器使用https而不是http。

回答

0

我发现它是Apache的错误配置的错误。

尝试Jamies解决方案,网站结束了重定向循环,因为即使请求是https,RequestHandler-> isSSL()也返回false。然后我发现没有设置$ _SERVER ['https'],$ _SERVER ['port']是80,而不是443。

在这一点上我已经把我的SSL指令在网站可用/默认情况下,

SSLEngine on 
SSLCertificateFile [path to cert file] 
SSLCertificateKeyFile [path to keyfile] 

移动SSL指令为子域的虚拟主机解决了这个问题,与重定向循环。

其实也解决了我最初的问题,因为该方法路由器:: URL检查$ _ SERVER [“HTTPS”如果设置生成以https开头的URL:否则只是HTTP:

我已经从测试Jameies解决方案和我自己的.htaccess中的重写规则,他们都在修复后按预期工作。

5

我正在一点猜测,但我怀疑这是这个非常重写规则多数民众赞成搞乱的东西了。

你应该“重定向”而不是“重写”。 Cake的生成链接通常是相对于根的,所以除非你传递“true”作为第二个参数,否则不要指定协议。

我也有Apache和80和443听,这样我至少可以回应不正确的请求。

这是我在我的AppController类做同样的事情代码:

function beforeFilter() { 
    parent::beforeFilter(); 
    $this->_setupSecurity(); 
} 

function _setupSecurity() { 
    $this->Security->blackHoleCallback = '_badRequest'; 
    if(Configure::read('forceSSL')) { 
     $this->Security->requireSecure('*'); 
    } 
} 

/** 
* The main SecurityComponent callback. 
* Handles both missing SSL problems and general bad requests. 
*/ 

function _badRequest() { 
    if(Configure::read('forceSSL') && !$this->RequestHandler->isSSL()) { 
     $this->_forceSSL(); 
    } else { 
     $this->cakeError('error400'); 
    } 
    exit; 
} 

/** 
* Redirect to the same page, but with the https protocol and exit. 
*/ 

function _forceSSL() { 
    $this->redirect('https://' . env('SERVER_NAME') . $this->here); 
    exit; 
} 

我也有我自己的配置“forceSSL”在bootstrap.php中用于打开这个和关闭取决于选项环境,所以需要将上述设置为true才能正常工作。

0

其示例在食谱 http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#usage

class AppController extends Controller { 
    // Add security component 
    public $components = array('Security'); 

    public function beforeFilter() { 
     $this->Security->blackHoleCallback = 'forceSSL'; 
     $this->Security->requireSecure(); 
    } 

    // Add this function in your AppController 
    public function forceSSL() { 
     return $this->redirect('https://' . env('SERVER_NAME') . $this->here); 
    } 
} 
已经提到
相关问题