Use .htaccess to redirect HTTP to HTTPs

后端 未结 17 1590
刺人心
刺人心 2020-12-02 05:51

Please, don\'t recommend me the long and very detailed thread with more than 173 upvotes. It didn\'t work for me. I have also tried many others (1, 2, 3, 4). They all give m

17条回答
  •  天命终不由人
    2020-12-02 06:33

    Here is an alternative solution you can use if you don't want to edit .htaccess:

    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 at the bottom of your theme functions.php

提交回复
热议问题