BoldGrid / w3-total-cache

GNU General Public License v2.0
152 stars 84 forks source link

.htaccess rules with brotli and gzip #519

Closed superpoincare closed 6 months ago

superpoincare commented 2 years ago

If brotli is available, this is the rule I get in .htaccess

image

This leaves out the case where "Accept-Encoding: br ... " isn't there in Request Headers, just gzip and deflate. In the case where browser doesn't accept brotli, non-gzipped file is sent.

The rules in the minify directory look fine and allow for both cases.

So need to add allow rules for gzip.

mavas84 commented 2 years ago

Hello @superpoincare

The code uses one or the other.

   /**
         * Set Accept-Encoding
         */
        if ( $config->get_boolean( 'browsercache.enabled' ) && $config->get_boolean( 'browsercache.html.brotli' ) ) {
            $rules .= "    RewriteCond %{HTTP:Accept-Encoding} br\n";

You can try adding this below the existing lines:

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=W3TC_ENC:_gzip]

So it will look like this:

RewriteCond %{HTTP:Accept-Encoding} br
RewriteRule .* - [E=W3TC_ENC:_br]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=W3TC_ENC:_gzip]

If this works you can try removing the word "else" from that line in wp-content/plugins/w3-total-cache/PgCache_Environment.php#L697

diff --git a/PgCache_Environment.php b/PgCache_Environment.php
index 7342a188..0d2c488f 100644
--- a/PgCache_Environment.php
+++ b/PgCache_Environment.php
@@ -694,7 +694,8 @@ class PgCache_Environment {
                        $rules .= "    RewriteCond %{HTTP:Accept-Encoding} br\n";
                        $rules .= "    RewriteRule .* - [E=W3TC_ENC:_br]\n";
                        $env_W3TC_ENC = '%{ENV:W3TC_ENC}';
-               } else if ( $config->get_boolean( 'browsercache.enabled' ) && $config->get_boolean( 'browsercache.html.compression' ) ) {
+               }
+               if ( $config->get_boolean( 'browsercache.enabled' ) && $config->get_boolean( 'browsercache.html.compression' ) ) {

Please let me know if this helps!

Thanks!

superpoincare commented 2 years ago

Hi @mavas84

Will try the fix.

The htaccess rule suggested may not work because browsers which accept both brotli and gzip have both of them in the Accept-Encoding header.

So the second RewriteCond rule would need some modification to check if br is not present in the header.

Otherwise brotli browsers will get gzip.