2
votes

I am trying to add GZip compression using Apache mod_deflate module. All contents get compressed except text files(".txt"). Also facing issues when compressing HTML files and JS files.

For Example: Let's say a script file ("main.js") hosted on http://example.com

The contents of file:

In case 1:

// Contents of main.js
console.log("Hello World");

console.log("Hello World");
   console.log("Hello World"); 
console.log("Hello World"); console.log("Hello World");

In case 2:

    // Contents of main.js
    Hello World! Hello World!
    Hello World! Hello World! Hello World!

    Hello World! Hello World! Hello World! Hello World!
 Random Contents

In case 1, When I visit http://example.com/main.js, it shows content-encoding: gzip.
In case 2, When I visit http://example.com/main.js, it doesn't show any content-encoding: gzip header.

So the GZip only works in Case 1 for .js files.

GZip not working on text files(.txt extension) and not showing any content-encoding: gzip header.

// .htaccess code

<IfModule mod_deflate.c>
<IfModule mod_mime.c>
    AddType text/plain .txt
</IfModule>
  SetOutputFilter DEFLATE
</IfModule>

My software list

  • Apache Version - 2.4.39
  • PHP Version - 7.3
  • OS - Windows 10
  • Software - Wamp 64Bit
1
If you remove the lineSetOutputFilter DEFLATE, are the js files still compressed? (My guess would be the compression is comming from somewhere else and your .htaccess has no effect.) - Roland Starke
No, After removing this GZip is completely disabled. - Kim

1 Answers

0
votes

If you add default mod_deflate and mod_gzip conf in .htaccess file some changes?

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_include handler ^cgi-script$
</IfModule>