Fix Content Encoding Error with Apache Served Brotli Files

.htaccessapache-2.4encodingmod-rewriterewrite

How can I get Apache to serve pre-compresses Brotli files?

With Chrome I am getting

net::ERR_CONTENT_DECODING_FAILED 200

I made my pre-compressed .br (Brotli) files from minimized .html files (.min.html). I used PeaZip (9.3.0) to generate these Brotli files. This is the process I used to make these files.

I host them alongside each other — all in the same directory. The naming convention is as such filename.html, filename.min.html and filename.html.br.

At this point, I am still hoping that these files can be used to have Apache (2.4) server pre-compressed — as is.

The rewrite rule is working. I go from https://sub.domain.com/v3/en/home/ to html/com___en___home.html.br using:

Alias "/v3/" "D:/www/v3/"
<Directory "D:/www/v3/">
    AddDefaultCharset UTF-8
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/v3(.*) [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)?$ [NC]
    RewriteRule ^([^.]+)/([^.]+)/([^.]+)/$ html/%3___$1___$2___$3.html

    # Serve the Brotli-compressed HTML files from the "static_html" directory
    RewriteRule ^(.+)\.html$ $1.html.br [L,T=text/html,E=no-brotli,E=no-gzip]
</Directory>

My .htaccess file inside that directory has

AddType application/brotli .br
AddEncoding br .br

# Disable Gzip compression for .br files
<IfModule mod_deflate.c>
    SetEnvIfNoCase Request_URI "\.br$" no-gzip
</IfModule>

Calling the page as such I am getting this:

FireFox Content Encoding Error

Changing my .htaccess file to this

AddType application/brotli .br
#AddEncoding br .br  <<< commented out

# Disable Gzip compression for .br files
<IfModule mod_deflate.c>
    SetEnvIfNoCase Request_URI "\.br$" no-gzip
</IfModule>

I get this

The byte stream was erroneous

and Firefox remarks

The byte stream was erroneous according to the character encoding that was declared. The character encoding declaration may be incorrect.

There is clearly a mismatch between the declared character encoding and the actual character encoding used.

At that stage my return headers are

HTTP/2 200 OK
date: Thu, 03 Aug 2023 13:40:54 GMT
server: Apache/2.4.39 (Win64) OpenSSL/1.1.1c PHP/8.1.10
strict-transport-security: max-age=31536000; includeSubdomains;
last-modified: Mon, 31 Jul 2023 16:39:50 GMT
etag: "2904-601cb14402180"
accept-ranges: bytes
content-length: 10500
cache-control: max-age=0
expires: Thu, 03 Aug 2023 13:40:54 GMT
x-xss-protection: 1; mode=block;
referrer-policy: no-referrer-when-downgrade
access-control-allow-methods: POST,GET,HEAD
vary: Origin
content-security-policy: default-src 'self' (removed content);
x-content-type-options: nosniff
content-type: text/html; charset=UTF-8
X-Firefox-Spdy: h2

These are things that ChartGTP suggested me doing (some do not relate because I am on a Windows setup.)

If you are encountering a "Content Encoding Error" while serving pre-compressed Brotli files on your Apache server, there are a few potential causes and solutions to consider:

  1. Ensure Brotli module is enabled: Verify that the mod_brotli module is enabled in your Apache server. Ensure that you have the following line in your Apache configuration to load the Brotli module:
LoadModule brotli_module modules/mod_brotli.so

If the module is not loaded, enable it using the appropriate command (e.g., sudo a2enmod brotli on Debian/Ubuntu or sudo systemctl enable brotli on CentOS/Fedora).

This makes little sens; as these are pre-compressed files. I did enable it.

  1. Check Brotli MIME type: Ensure you have set the correct Brotli MIME type in your Apache configuration. Add the following line:
AddType application/brotli .br

This tells Apache to associate the ".br" extension with Brotli-compressed files.

  1. Verify Brotli compression: Ensure that your Brotli compression is working correctly and the files are properly compressed. If you are using a tool or script to pre-compress the files, double-check that it is producing valid Brotli-compressed files.

Number 3 is still on my to-do list.

  1. File permissions: Check the file permissions of your pre-compressed Brotli files. Ensure that they have the necessary permissions to be served by Apache.

  2. Content-Length header: Ensure that the Content-Length header is being correctly set in the response for the pre-compressed Brotli files. The absence or incorrect value of this header can lead to issues with content delivery.

Number 5 I did not look into.

  1. Content-Encoding header: Confirm that the Content-Encoding header for Brotli-compressed files is set to "br" in the response headers. If the header is missing or set to a different value, it may cause the "Content Encoding Error."

If I set the header to Brotli, using AddEncoding br .br I get the above mentioned Content Encoding Error.

I tried AddEncoding hello .br and got this output:

hello test

So I need it set to AddEncoding br .br — but this gives me the Content Encoding Error.

  1. Clear browser cache: If you have recently made changes to your server's configuration or Brotli compression, clear your browser's cache to ensure that the updated settings are retrieved.

  2. Check for conflicting compression methods: Ensure there are no conflicts with other compression methods (e.g., Gzip) that might interfere with Brotli compression. If Gzip is also enabled, check for proper content negotiation.

Disabled that with

# Disable Gzip compression for .br files
<IfModule mod_deflate.c>
    SetEnvIfNoCase Request_URI "\.br$" no-gzip
</IfModule>
  1. Check server logs: Review your Apache server logs, especially the error logs, for any relevant error messages that might provide additional insights into the issue.
    After implementing these checks and potential solutions, restart your Apache server to apply the changes. If the problem persists, consult the Apache documentation and consider seeking help from relevant communities or forums for further assistance in troubleshooting the specific issue you are facing.

My log file records

IP - - [02/Aug/2023:22:09:35 -0700] "GET /v3/en/home/ HTTP/2.0" 200 10500

Thank you for your help.

Best Answer

I discovered that manually created Brotli files, using the PeaZip UI, do not cause this encoding problem.

At first I was using the stronger compression --large_window=27 removing it made it work. This works:

Brotli_command = """C:\Program Files\PeaZip\res\bin\brotli\brotli.exe"" -9 " & _
  " """ & f_min_html & """ -o """ & f_html_br & """"