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

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Jon Brown says

    Lol, I was literally try the get http>Https redirects set via the WPE portal working an hour ago… I didn’t succeed, so I’ll give this a try.

  2. Peter Wise says

    Hey – works great – thank you!

    The only thing I would add is since SEO is not a consideration on a staging site, you might do a 302 instead of a 301 redirect. This won’t get cached by the browser, which can be annoying if you’re trying to go back to allowing http:// for any reason in the future. Or if something is wrong with the code or your setup you don’t endup with broken redirects cached.

  3. Meredith says

    Worked on my staged site on WP Engine (wanted to update because you posted this 3 years ago). I needed to test a payment gateway, but it wouldn’t let me test it unless there was a secure connection. Much appreciated!