Apache GZip not working text files and js files? - php

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

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>

Related

WordPress compression not working on localhost, XAMPP

I need some help with WordPress speed optimization via .htaccess.
For now, the website is still running on localhost, XAMPP, Apache v3.2.4.
What I did / tried:
turn on GZip in .htaccess
enable mod deflate in .htacess
place .htaccess in WordPress root directory
Google Lighthouse Speed Test via the Chrome dev tools does not detect:
Compression (message: Enable text compression)
When I examine the response header via dev tools, there's no cache-control: ... entry.
I would appreciate some help on how to fix these problems. I figure that I went wrong somewhere in my setup, but I'm not sure where.
So, here's what I did for COMPRESSION in .htacces:
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -f
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.gz [L]
# Prevent mod_deflate double gzip
RewriteRule \.gz$ - [E=no-gzip:1]
<FilesMatch "\.gz$">
# Serve correct content types
<IfModule mod_mime.c>
# (1)
RemoveType gz
# Serve correct content types
AddType text/css css.gz
AddType text/calendar ics.gz
AddType text/javascript js.gz
AddType application/json json.gz
AddType text/html html.gz
AddType image/svg+xml svg.gz
# Serve correct content charset
AddCharset utf-8 .css.gz \
.ics.gz \
.js.gz \
.json.gz
</IfModule>
# Force proxies to cache gzipped and non-gzipped files separately
Header append Vary Accept-Encoding
</FilesMatch>
# Serve correct encoding type
AddEncoding gzip .gz
</IfModule>

Mamp - Getting Gzip to work

How do you get gzip to work on a Wordpress site setup in MAMP?
I've got gulp creating the file /dist/css/main.css.gz and have added it to the header PHP file of my wordpress site.
I then lose all of my styling. I'm assuming this is because I haven't enabled it in my .htaccess? So I found the following code to add to my .htaccess:
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
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>
And it still does not work.
I'm not sure why you'd want to store your files as gzip archives, but this is not the way to do it. AddOutputFilterByType DEFLATE text/css will compress the file before sending it. To decompress an existing file, something like this will work (tested with 2.2):
<Directory />
AddOutputFilter INFLATE gz
RemoveType .gz
AddType text/css .css.gz
</Directory>
Test output:
$ echo "body {color:black; background:white;}" > foo.css
$ gzip foo.css
$ curl http://localhost/foo.css.gz
body {color:black; background:white;}
$ curl -I http://localhost/foo.css.gz
HTTP/1.1 200 OK
Date: Fri, 07 Jul 2017 23:01:22 GMT
Server: Apache/2.2.15 (Scientific Linux)
Last-Modified: Fri, 07 Jul 2017 22:59:22 GMT
Content-Length: 38
Content-Type: text/css
$
Of course, this interferes with your ability to serve actual gzip files, and you'll need to manually specify MIME types for each compressed file.
Also, if you're controlling the server you should not be using .htaccess files. They're slow and present a security risk. Put this in the server configuration!

GZIP Compression Not Working For Wordpress Site

I'm implementing a website in wordpress. When I test my site on google page speed, It's looking horrible. I'm getting some compression issues.
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yK/r/XVnmkWxbmyb.js could save 152KiB (74% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yz/r/7emJbLsMYck.js could save 123.6KiB (72% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yY/r/MYnSaHkqljr.js could save 121.7KiB (70% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yh/r/Sst4Eu6wCBO.js could save 69.3KiB (70% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/y6/r/luizkbRWOQS.js could save 24.2KiB (67% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yI/r/oDzewPwhRt1.js could save 21.7KiB (74% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yP/r/gcdIMTVPpBq.css could save 17.3KiB (77% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yf/r/nrh6Sehx1Iz.css could save 15.4KiB (73% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yV/r/1pZAFn_KQPn.css could save 14.2KiB (74% reduction).
Compressing http://www.advancebestmaid.com.sg/ could save 12.7KiB (72% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/ya/r/acNHHHLf-Ph.js could save 11.3KiB (69% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yR/r/qGNhIQHDeK2.js could save 9.3KiB (63% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yr/r/V2fXuYrZ-4H.css could save 3.6KiB (71% reduction).
Compressing https://static.xx.fbcdn.net/rsrc.php/v2/yY/r/Uj-Y3GNRxq1.js could save 3.3KiB (59% reduction).
I already checked with my server to make sure that, GZIP is enabled. And I'm using Yoast SEO plugin to enable HTTP (gzip) compression on my site.
This is my .htaccess code:
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
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>
I cannot understand why the compression not working. Please help me guys.
URL: mysite
add these lines too
<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>
You cannot compress external (loaded from external domains) javascripts or css. Almost all those files are loaded from https://static.xx.fbcdn.net/. You have to complain about it to Facebook. It is their domain.

PHP disable zlib.output_compression

My site has zlib.output_compression set to On in the ini file, it will compress all response when sending to the browser.
How to disable the compression only for binary file, PDF file and excel file so it will output to the browser correctly when download a file ?
Use a .conf file in your server, restricting compression to the right MIME types
for example in /etc/httpd/conf.d/enable_mod_deflate.conf:
# mod_deflate configuration
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xml+rss
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE image/png
AddOutputFilterByType DEFLATE image/gif
AddOutputFilterByType DEFLATE image/jpeg
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
<IfModule mod_headers.c>
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
don't forget to restart your servers
sudo service httpd restart
zlib.output_compression = on/off
and in PHP
ini_set("zlib.output_compression", "On");
ini_set("zlib.output_compression", "Off");
Check this one for more details - http://php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression

How do I site optimization php files with gzip compression using .htaccess or php.ini?

I'm using .htaccess provided by http://html5boilerplate.com/ which is correctly compressing the .html, .css, & .js files but I have not been able to get the .php files to compress unless I add <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?> to the top of the page. The last thing I want to do is add that line to a hundred plus pages.
I've been using the following two sites to verify the gzip compression:
http://www.whatsmyip.org/http-compression-test/
http://www.gidnetwork.com/tools/gzip-test.php
The following suggestions have not worked either:
Gzip compression through .htaccess not working
How do you use gzip in .htaccess or php.ini?
I've also tried the following (http://perishablepress.com/fast-effective-php-compression/):
<?php // placed in a gzip_start.php file
ob_start("ob_gzhandler");
?>
<?php // placed in a gzip_stop.php file
ob_flush();
?>
# Placed in the .htaccess filew
# dual file includes for PHP compression
php_value auto_prepend_file /specify/full/path/to/gzip_start.php
php_value auto_append_file /specify/full/path/to/gzip_stop.php
Here's the Gzip section of my .htaccess file:
# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s,?\s(gzip|deflate)?|X{4,13}|~{4,13}|-{4,13})$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(html|css|javascript|plain|x(ml|-component))/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/text text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
</IfModule>
# Webfonts and SVG:
<FilesMatch "\.(ttf|otf|eot|svg|php)$" >
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
Edit Your .htaccess File like This
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# Or, compress certain file types by extension:
&ltfiles *.html &gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.css&gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.js &gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.img &gt
SetOutputFilter DEFLATE
&lt/files&gt
If you have views with .php extension. You will have to add this to .htaccess file
<files *.php >
SetOutputFilter DEFLATE
</files>

Categories