Redirect WordPress HTTPS to HTTP without editing .htaccess

Here is a solution you can use if you want to force your WordPress website to redirect all HTTPS pages without having to edit the .htaccess file.

Copy this code and paste it at the bottom of your theme functions.php:

add_action( 'template_redirect', 'nonhttps_template_redirect', 1 );
function nonhttps_template_redirect() {
  if ( is_ssl() && !is_admin() ) {
    if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
      wp_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ), 301 );
      exit();
    } else {
      wp_redirect( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
      exit();
    }
  }
}

This is the most reliable method for the redirect behavior as for example there are several WordPress plugins that can overwrite changes to the .htaccess file.

Alternatively you may want to redirect your WordPress website from HTTP to HTTPS:

add_action( 'template_redirect', 'nonhttps_template_redirect', 1 );
function nonhttps_template_redirect() {
  if ( is_ssl() ) {
    if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'https' ) ) {
      wp_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ), 301 );
      exit();
    } else {
      wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
      exit();
    }
  }
}

You can place this code at the bottom of your theme functions.php

Leave A Comment

Your email address will not be published. Required fields are marked *