Skip to content

Commit 5d508fb

Browse files
authored
Update force-https.php
1 parent cfac88e commit 5d508fb

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

force-https.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ function force_https_filter_home( $value ) {
3434
add_filter( 'pre_option_home', 'force_https_filter_home' );
3535
add_filter( 'pre_option_siteurl', 'force_https_filter_home' );
3636

37-
// force https redirect on frontend, admin, and login
37+
// enforce https by redirecting non-ssl requests on frontend, admin, and login pages
3838
function force_https_redirect() {
39-
if ( ! is_ssl() && ! defined( 'WP_CLI' ) && ! headers_sent() ) {
40-
wp_redirect( set_url_scheme( home_url( $_SERVER['REQUEST_URI'] ), 'https' ), 301 );
41-
exit;
39+
40+
// exit if already using https, headers are sent, or running via cli
41+
if ( is_ssl() || headers_sent() || defined( 'WP_CLI' ) ) {
42+
return;
4243
}
44+
45+
// redirect to https version of the requested url with a permanent redirect
46+
wp_redirect( set_url_scheme( home_url( $_SERVER['REQUEST_URI'] ), 'https' ), 301 );
47+
exit;
4348
}
4449

45-
// apply https redirect to all key areas
46-
foreach ( array( 'init', 'admin_init', 'login_init' ) as $hook ) {
50+
// apply https redirect during initialization, admin, and login
51+
foreach ( [ 'init', 'admin_init', 'login_init' ] as $hook ) {
4752
add_action( $hook, 'force_https_redirect', 10 );
4853
}
4954

0 commit comments

Comments
 (0)