cliff
8/29/2013 - 2:28 PM

Get SSL / HTTPS to work properly on WP Engine type hosts. Jason Cohen, Aug 28 05:29 pm (CDT): This is because the SSL processing is do

Get SSL / HTTPS to work properly on WP Engine type hosts.

Jason Cohen, Aug 28 05:29 pm (CDT): This is because the SSL processing is done at the front-end caching layer and not at the back-end PHP layer. By the time the request gets back there, the protocol is HTTP again rather than HTTPS.

However, WordPress also defines a PHP variable saying whether in fact the connection is HTTPS -- $_SERVER['HTTPS'].

Also there are tricks for this well-known problem (WP Engine is far from the only host to do this!) as described here: http://snippets.webaware.com.au/snippets/wordpress-is_ssl-doesnt-work-behind-some-load-balancers/

Hope this helps; if you need something more specific, let us know.

--Jason

<?php
/*
Plugin Name: Force SSL URL Scheme
Plugin URI: https://gist.github.com/cliffordp/6378826
Description: Force the protocol scheme to be HTTPS when is_ssl() doesn't work. Recommended to me by Jason Cohen of WP Engine on 2013-08-28.
Version: 1.0.0
Author: WebAware
Author URI: http://www.webaware.com.au/
*/

/*
copyright (c) 2013 WebAware Pty Ltd (email : rmckay@webaware.com.au)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// if site is set to run on SSL, then force-enable SSL detection!
if (stripos(get_option('siteurl'), 'https://') === 0) {
    $_SERVER['HTTPS'] = 'on';

    // add JavaScript detection of page protocol, and pray!
    add_action('wp_print_scripts', 'force_ssl_url_scheme_script');
}

function force_ssl_url_scheme_script() {
?>
<script>
if (document.location.protocol != "https:") {
    document.location = document.URL.replace(/^http:/i, "https:");
}
</script>
<?php
}