	 * Repair standalone malformed protocols without touching normal URLs.
	 */
	$content = preg_replace(
		'#(?<!:)https?:/(?!/)#i',
		function ( $match ) {
			return ( 0 === stripos( $match[0], 'http:/' ) )
				? 'http://'
				: 'https://';
		},
		$content
	);

	return $content;
}

/*
 * Run late so other shortcodes/page builders have already generated
 * their frontend HTML.
 */
add_filter(
	'the_content',
	'researchbrains_fix_malformed_external_links',
	99999
);

add_filter(
	'the_excerpt',
	'researchbrains_fix_malformed_external_links',
	99999
);

add_filter(
	'widget_text',
	'researchbrains_fix_malformed_external_links',
	99999
);

add_filter(
	'elementor/frontend/the_content',
	'researchbrains_fix_malformed_external_links',
	99999
);

/**
 * Also repair direct requests to an already-indexed malformed URL.
 *
 * Example:
 * /%22https:/link.springer.com/journal/13042/
 *
 * redirects to:
 * https://link.springer.com/journal/13042/
 */
function researchbrains_redirect_malformed_external_request() {

	if ( is_admin() || empty( $_SERVER['REQUEST_URI'] ) ) {
		return;
	}

	$request_uri = wp_unslash( $_SERVER['REQUEST_URI'] );

	/*
	 * Decode once for matching while preserving the original request.
	 */
	$decoded = rawurldecode( $request_uri );

	/*
	 * Match a path that starts with an encoded/decoded quote followed
	 * by an external absolute URL.
	 */
	if ( preg_match(
		'#^/(?:%22|["\']|&quot;)(https?):/{1,2}([^/?#]+)(/[^?]*)?(?:\?(.*))?$#i',
		$request_uri,
		$matches
	) ) {

		$scheme = strtolower( $matches[1] );
		$host   = $matches[2];
		$path   = ! empty( $matches[3] ) ? $matches[3] : '/';
		$query  = ! empty( $matches[4] ) ? '?' . $matches[4] : '';

		$target = $scheme . '://' . $host . $path . $query;

		wp_redirect( esc_url_raw( $target ), 301 );
		exit;
	}

	/*
	 * Fallback for encoded paths after one decode.
	 */
	if ( $decoded !== $request_uri && preg_match(
		'#^/["\']?(https?):/{1,2}([^/?#]+)(/[^?]*)?(?:\?(.*))?$#i',
		$decoded,
		$matches
	) ) {

		$scheme = strtolower( $matches[1] );
		$host   = $matches[2];
		$path   = ! empty( $matches[3] ) ? $matches[3] : '/';
		$query  = ! empty( $matches[4] ) ? '?' . $matches[4] : '';

		$target = $scheme . '://' . $host . $path . $query;

		wp_redirect( esc_url_raw( $target ), 301 );
		exit;
	}
}

add_action(
	'template_redirect',
	'researchbrains_redirect_malformed_external_request',
	1
);
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://researchbrains.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://researchbrains.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://researchbrains.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://researchbrains.com/wp-sitemap-posts-sureforms_form-1.xml</loc></sitemap><sitemap><loc>https://researchbrains.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://researchbrains.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
