ssl redirect function is not working

Question

I want to redirect when our website is on HTTP to HTTPS: I wrote a function but it is not working. please help me.

public function add_header(){
    if($this->current_domain == "http://". $this->domain || $this->current_domain == "https://". $this->domain){
        
        if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']=="https"){ 

            return true;
        
        } elseif (isset($_SERVER['HTTPS'])) {
            
            $this->redirect = get_option('ssl-redirect-check');
            
            if ( 'on' === strtolower( $_SERVER['HTTPS'] && strpos( home_url(), 'https' ) == false )) {
                    $ssl_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
                    wp_redirect($ssl_url, 301);
                    exit();
            }
            if ( '1' == $_SERVER['HTTPS'] ) {
                    $ssl_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
                    wp_redirect($ssl_url, 301);
                    exit();
            }
        } else {
            if ($this->redirect == "yes"){
                    $ssl_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
                    wp_redirect($ssl_url, 301);
                    exit();
            }
        }
    } else {
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: ". $this->current_domain . $this->uri );
        die();
    }

}
0
sunfarma hulla 2 months 0 Answers 19 views 0

Leave an answer