Skip to content

Instantly share code, notes, and snippets.

Last active September 3, 2023 00:05
  • Star 53 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
For WordPress, force the protocol scheme to be HTTPS when is_ssl() doesn't work, e.g. on a load-balanced server where _SERVER['HTTPS'] and _SERVER['SERVER_PORT'] don't indicate that SSL is being used. NB: may not be needed now, see SSL Insecure Content Fixer and HTTP Detection:
Plugin Name: Force SSL URL Scheme
Plugin URI:
Description: Force the protocol scheme to be HTTPS when is_ssl() doesn't work
Version: 1.0.0
Author: WebAware
Author URI:
copyright (c) 2013 WebAware Pty Ltd (email :
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
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() {
if (document.location.protocol != "https:") {
document.location = document.URL.replace(/^http:/i, "https:");
Copy link

Thanks. I ended up adding this as a mu-pluging (adding the script to: wp-content/mu-plugins/index.php)

Copy link


Copy link

FlatText commented Apr 2, 2023

Wow. Added this as a plugin since I was all out of ideas. Have a critical gov site that refused to connect to the WP backend, 'Too many redirects'. Site worked okay, but no admin access.

Created this , restart apache, and holy sausage. It worked. Thank you

Copy link

I didn't realize I had left my site behind a proxy wall at cloudflare. Found a little better fix in the wp-config file at the top:

define('FORCE_SSL_ADMIN', false);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment