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 a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments

  1. 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. 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.

Leave A Reply