Question

I am building a website for my company which loads over HTTP inside the company and over HTTPS outside company's network. This is a compulsion and there is no way we can avoid it. The site needs to work over both HTTP and HTTPS.

This mixed HTTP & HTTPS is giving a huge problem to us. I tried using plugins which make the URLs relative. But some URLs are still absolute and the admin panel doesn't open properly over HTTPS.

Is there any hack using which we can remove HTTP from the site URL and have just two slashes (//) in it? This will solve our problem, at least for front end.

Thanks in advance. Your help is appreciated.

Was it helpful?

Solution

To remove http: and https: from all links, use the following script:

add_action( 'plugins_loaded', 'wpse_232287_init' );

function wpse_232287_init() { // Initiate the function
    ob_start( 'wpse_232287_remove_http' );
}

function wpse_232287_remove_http( $buffer ) {
    // Check for a Content-Type header, only apply rewriting to "text/html" or undefined
    $headers = headers_list();
    $content_type = null;

    foreach ( $headers as $header ) {
        if (strpos( strtolower( $header ), 'content-type:' ) === 0 ) {
            $pieces = explode( ':', strtolower( $header ) );
            $content_type = trim( $pieces[1] );
            break;
        }
    }

    if ( is_null( $content_type ) || substr( $content_type, 0, 9 ) === 'text/html' ) { // Replace 'href'/'src' attributes within script/link/base/img tags with '//'
        $return = preg_replace( "/(<(script|link|base|img|form)([^>]*)(href|src|action)=[\"'])https?:\\/\\//i", "$1//", $buffer );
        if ( $return ) { // On regex error, skip overwriting content
            $buffer = $return;
        }
    }
    return $buffer;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top