WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation


wp-rocket-wordpress WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation AMP (Accelerated Mobile Pages) cloudflare

The AMP (Accelerated Mobile Pages) is a very strict HTML5 specification. If there are critical issues with AMP, Google will not index them. For example, in AMP, you are not supposed to use // – the protocol to represent a HTTP or HTTPS url depending on the current page protocol (but in HTML5, you can)

amp-https WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation AMP (Accelerated Mobile Pages) cloudflare

CloudFlare HTTPS Rewrite Fails AMP Validation

Unfortunately, the CloudFlare WP Rocket Plugin’s [Automatic HTTPS Rewrites] module fails the AMP validation, so you must turn it off otherwise, you will get errors like:

  • The tag ‘script’ is disallowed except in specific form.
  • The mandatory tag ‘amphtml engine v0.js script’ is missing or incorrect.

Update: It turns out it is NOT CloudFlare’s fault. It is the WP Rocket WordPress Plugin forcibly enables HTTPS rewrite (to //) even on AMP pages.

WP Rocket Plugin’s [Automatic HTTPS Rewrites] module rewrites all images, stylesheets and scripts from using either http:// or https:// to using just // to support Flexible SSL.

wprocket-plugin-https-protocol-rewrite WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation AMP (Accelerated Mobile Pages) cloudflare

wprocket-plugin-https-protocol-rewrite

CloudFlare has run the same HTML through a test domain on Cloudflare with Automatic HTTP Rewrites enabled and has confirmed that it is not caused by CloudFlare.

  • CloudFlare disables rewrite features for AMP specifically. But WP-Rocket Plugin has this option in the dashboard that turns it back on.
  • Automatic HTTPS Rewrites does not rewrite URLs to // – it only uses full absolute links e.g. https://
cloudflare-automatic-https-rewrites WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation AMP (Accelerated Mobile Pages) cloudflare

cloudflare-automatic-https-rewrites

If you are using WP-Rocket Plugin, then you can write a plugin or add the following function to your template e.g. functions.php. The following only disables this ‘Automatic HTTPS Rewrite’ in the AMP pages e.g. is_amp_endpoint()

1
2
3
4
5
6
7
8
9
10
add_action( 'wp', 'disable_cloudflare_automatic_https_rewrite_amp' );
function disable_cloudflare_automatic_https_rewrite_amp() {
    if ( defined( 'AMP_QUERY_VAR' ) && function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
        $do_rocket_protocol_rewrite = apply_filters( 'do_rocket_protocol_rewrite', false );
        if ( ( get_rocket_option( 'do_cloudflare', 0 ) && get_rocket_option( 'cloudflare_protocol_rewrite', 0 ) || $do_rocket_protocol_rewrite ) ) {
            remove_filter( 'rocket_buffer', '__rocket_protocol_rewrite', PHP_INT_MAX );
            remove_filter( 'wp_calculate_image_srcset', '__rocket_protocol_rewrite_srcset', PHP_INT_MAX );
        }
    }
}
add_action( 'wp', 'disable_cloudflare_automatic_https_rewrite_amp' );
function disable_cloudflare_automatic_https_rewrite_amp() {
	if ( defined( 'AMP_QUERY_VAR' ) && function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
		$do_rocket_protocol_rewrite = apply_filters( 'do_rocket_protocol_rewrite', false );
		if ( ( get_rocket_option( 'do_cloudflare', 0 ) && get_rocket_option( 'cloudflare_protocol_rewrite', 0 ) || $do_rocket_protocol_rewrite ) ) {
			remove_filter( 'rocket_buffer', '__rocket_protocol_rewrite', PHP_INT_MAX );
			remove_filter( 'wp_calculate_image_srcset', '__rocket_protocol_rewrite_srcset', PHP_INT_MAX );
		}
	}
}

WP-Rocket is not based on Cloudflare – but it has options for configuring Cloudflare.

  • Automatic HTTPS Rewrites on Cloudflare is safe to use with AMP
  • WP-Rocket Plugin has their own logic for HTTPS rewriting that should be disabled if you use AMP

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
715 words
Last Post: How to Enable AMP with WordPress? AMP Optimisation Techniques with WordPress
Next Post: Words Typed in One-Row's American Keyboard

The Permanent URL is: WP-Rocket Plugin Automatic HTTPS Rewrite Fails AMP Validation

Leave a Reply