1

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.

2
  • 1
    Probably gonna be a server setting, not a Cake thing. Commented Dec 9, 2014 at 4:32
  • 1
    "aws load balancer force https" - first result from Google Commented Dec 9, 2014 at 5:34

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

4 Comments

is define('IS_IN_PRODUCTION', 0); must be 1 when deploying to production env?
Yes, IS_IN_PRODUCTION must be 1 to be active.
This also works in CakePHP 3. All of the other methods I've read about caused a redirect loop on my production host. Thanks!!
Another option might be to use a separate config file that is required as index.php is not ignored by git by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.