Force SSL when using WPEngine staging or transferrable installs

I use WPEngine as my development environment for client sites. Using git to push code changes and Migrate DB Pro to push content changes makes development a breeze.

There is one small quirk that I’ve noticed. WPEngine forces SSL on the backend, but lets you use either http or https on the frontend. Even if you set all the URLs in your site to https, if you type in the http version of the URL you are not redirected. This isn’t an issue on production sites because you can customize the SSL settings, but for transferrable or staging installs the SSL page is inaccessible.

This can be a bit confusing to clients if they type in the URL and no longer see the “Secure” message in their browser. But it can also cause technical issues with the site. If you’re using admin-ajax for anything – for instance, sending emails in EA Share Count – these won’t work because there’s a domain mismatch.

You could install a plugin that forces SSL, but I worry I’d forget to deactivate the plugin before launching the site in an environment where the client doesn’t already have SSL setup.

Instead, I use the following code. This can be added to your core functionality plugin or a standalone plugin.

<?php
/**
* Force SSL on WPEngine install
*
* @author Bill Erickson
* @see https://www.billerickson.net/force-ssl-on-wpengine
*
*/
function be_force_ssl_on_wpengine() {
if( strpos( home_url(), 'wpengine' ) && ! is_ssl() ) {
wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
exit();
}
}
add_action( 'template_redirect', 'be_force_ssl_on_wpengine' );
view raw plugin.php hosted with ❤ by GitHub

Tutorial

Receive New Posts by Email

Comments

Leave a comment