简体   繁体   中英

How to force https in CakePHP without redirect from http?

I want to use https for my CakePHP application.

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 )

But the problem is I'm using load balancer (AWS), so I faced with the problem if I use redirect (redirect loop).

Would anyone have experience with this issue?

How to force https in CakePHP without redirect from http ?

Thanks so much.

I use CakePHP 2 on server without https, but with activated cloudflare (with https). So CloudFlare give me https to the end user. This is what I have added to my /app/webroot/index.php to make it work:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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