简体   繁体   English

如何在CakePHP中强制使用https而无需从http重定向?

[英]How to force https in CakePHP without redirect from http?

I want to use https for my CakePHP application. 我想对我的CakePHP应用程序使用https。

I found some articles explain how to force https in CakePHP ( http://blog.andolasoft.com/2013/07/ssl-authentication-using-security-component-in-cakephp.html ) 我发现一些文章介绍了如何在CakePHP中强制使用https( http://blog.andolaso​​ft.com/2013/07/ssl-authentication-using-security-component-in-cakephp.html

But the problem is I'm using load balancer (AWS), so I faced with the problem if I use redirect (redirect loop). 但是问题是我正在使用负载平衡器(AWS),所以如果我使用重定向(重定向循环),则会遇到问题。

Would anyone have experience with this issue? 有人会遇到这个问题吗?

How to force https in CakePHP without redirect from http ? 如何在不从http重定向的情况下在CakePHP中强制使用https?

Thanks so much. 非常感谢。

I use CakePHP 2 on server without https, but with activated cloudflare (with https). 我在没有https的服务器上使用CakePHP 2,但在激活的cloudflare(带有https)上使用。 So CloudFlare give me https to the end user. 所以CloudFlare给我https给最终用户。 This is what I have added to my /app/webroot/index.php to make it work: 这是我添加到我的/app/webroot/index.php中以使其工作的内容:

function isSSL(){
    if( !empty( $_SERVER['https'] ) )
        return true;
    if( !empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' )
        return true;
    return false;
}

define('IS_IN_PRODUCTION', 1);
if( !isSSL() && IS_IN_PRODUCTION > 0){
    header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    exit();
}

I hope this can help! 希望对您有所帮助!

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

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