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!
Related
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>
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>
I have this in my .htaccess:
# Insert filter on all content
SetOutputFilter DEFLATE
# Insert filter on selected content types only
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
And all my static content returns Content-Encoding: gzip. But my PHP-generated pages doesn't and GTmetrix site says that I need to compress this content (my web-pages). How to enable gzip for PHP -generated pages?
I have the same .htaccess settings in another project and web-pages compression works on it.
My site uses Magento 2 engine.
The problem was in shared hosting settings. I contacted the support and they enabled compression for pages.
I am using magento 1.8.1 and it is very slow. I want to optimize this, for that I am using some free module for HTML, CSS and JS minification but its doesn't work.After using that it create lots of error. I've referred website such like after merge JS, the custom module in front-end is stop working. I've checked lots of tutorial also for gzip compression and enabling Mod_deflect, I've followed all the step like put some code in .htaccess page like
<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>
But that also didn't work.
this is my website: Link
Please help me into this.
Magento has the option to merge css and js files which works for me. I minimise the css and js files I use but I DON'T minimise the standard Magento files (epsecially prototype. I've read that this can break things).
As for compression, it's possible that you can't use the standard htaccess approach. See Compressing js & css files where mod deflate not available for a way around it.
Try to merge CSS and JS byon the admin panel System > Configuration > Developer (Be careful with the JS, the site stop working right in my case).
The thing that change everything for me it's changing the cache module. Magento use a cache module witch use the Zend_Cache library and create a huge numbers of files that makes the site terrible slow. You can easy replace it with this:
Cm_Cache_Backend_File
or even better, magento is prepeared to work with Redis. With this feature the cache goes to RAM instead the File System.
Good luck!
Here In a Wordpress site, I have enabled gzip compression at .htaccess. I have checked gzip compression and Content-Encoding appears correctly here.
But when checking compression at Chrome's developer tool, it doesn't show Content-Encoding on Response section:
Here are the .htaccess compression statements:
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
AddType x-font/otf .otf
AddType x-font/ttf .ttf
AddType x-font/eot .eot
AddType x-font/woff .woff
AddType image/x-icon .ico
AddType image/png .png
Why Chrome won't show gzip information?
It seems that Google Chrome, as Mozilla Firefox already decodes content. But there is a difference between them.
In Request Headers the extra field Accept-Encoding: "gzip, deflate" shows up but in Response Headers there is no Content-Encoding: gzip
Using Fiddler is possible to click in Transformer button (from Response Headers section) and choose to encode content. After that click in Raw button to check if content-encoding is being used.
So Fiddler is nicer to check for response headers.