WordPress – Enable GZIP via .htaccess

WordPress is a highly popular blogin and Content Management System. It allows you to easily develop and run your web site with a lot of control over any aspect of you web site.

In most cases your web site will be heavily overloaded with scripts, images etc… and this rapidly slows opening of a website in visitors browser. Besides necessity to optimize your code and graphics ( this is always MUST ) there is one more trick to accelerate loading speed of your website. We talking about GZIP compression method, where your web server have feature to compress output of your web site and to deliver to your visitors. This is very powerful method as in most cases your output can me shrinked to up to 70% of original output size. So, why not to use it!?

If you are not sure whether your web site is GZIP enable or not, it is good to check it first, we recommend to visit www.checkgzipcompression.com and enter URL of your website for checking. If your website is not GZIP enabled, it is a right time to do it.There is several ways to enable GZIP compression, you can do it with WordPress plugins ofcourse, but that is too easy and I find that method to enable compression via .htaccess gives more flexibility and safety to this process. Let`s start.

First you have to connect via FTP to your web hosting folder where your WordPress is installed. Lets say this is Public_html folder in my case.

WordPress Enable GZIP figure 1

Download it to your PC and open .htaccess file in some text editor (you can use plain NOTEPAD for this purpose ). BEFORE DO ANY CHANGING IN .HTACCESS FILE BE SURE YOU MAKE SAFE BACKUP COPY OF .HTACCESS FILE.

When you open it in let`s say Notepad you will see standard WordPress .htaccess content like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Do not make any changes in this part, just leave like it is. All we are going to do is just adding commands after this text.
So, after part </IfModule> add following:

# BEGIN GZIP COMPRESSION
<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 handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# END GZIP COMPRESSION

Here we enable GZIP compression for our WordPress. But is not all. Now we want to enable DEFLATE COMPRESSION.
After # END GZIP COMPRESSION add following commands:

# BEGIN DEFLATE COMPRESSION
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE “application/atom+xml” \
“application/javascript” \
“application/json” \
“application/ld+json” \
“application/manifest+json” \
“application/rdf+xml” \
“application/rss+xml” \
“application/schema+json” \
“application/vnd.geo+json” \
“application/vnd.ms-fontobject” \
“application/x-font-ttf” \
“application/x-javascript” \
“application/x-web-app-manifest+json” \
“application/xhtml+xml” \
“application/xml” \
“font/eot” \
“font/opentype” \
“image/bmp” \
“image/svg+xml” \
“image/vnd.microsoft.icon” \
“image/x-icon” \
“text/cache-manifest” \
“text/css” \
“text/html” \
“text/javascript” \
“text/plain” \
“text/vcard” \
“text/vnd.rim.location.xloc” \
“text/vtt” \
“text/x-component” \
“text/x-cross-domain-policy” \
“text/xml”
</IfModule>
# END DEFLATE COMPRESSION

Here we added comands to deflate scripts and other stuff that works under the hood so our WordPress output will be musch smaller comparing to non gzip and non deflatet original output.

So your complete .htaccess file should be like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# BEGIN GZIP COMPRESSION
<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 handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# END GZIP COMPRESSION

# BEGIN DEFLATE COMPRESSION
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE “application/atom+xml” \
“application/javascript” \
“application/json” \
“application/ld+json” \
“application/manifest+json” \
“application/rdf+xml” \
“application/rss+xml” \
“application/schema+json” \
“application/vnd.geo+json” \
“application/vnd.ms-fontobject” \
“application/x-font-ttf” \
“application/x-javascript” \
“application/x-web-app-manifest+json” \
“application/xhtml+xml” \
“application/xml” \
“font/eot” \
“font/opentype” \
“image/bmp” \
“image/svg+xml” \
“image/vnd.microsoft.icon” \
“image/x-icon” \
“text/cache-manifest” \
“text/css” \
“text/html” \
“text/javascript” \
“text/plain” \
“text/vcard” \
“text/vnd.rim.location.xloc” \
“text/vtt” \
“text/x-component” \
“text/x-cross-domain-policy” \
“text/xml”
</IfModule>
# END DEFLATE COMPRESSION

When you are finishid with editing save your .htaccess file and prepare for upload to your web server. Again connect via FTP and before uploading new .htaccess file I recommend to RENAME YOUR EXISTING .HTACCESS file that is allready working on your web server. Rename it to something like HTACCESS.OLD (pay attention that in renamed file there is no DOT at the begining of filename ).

Now you can upload your new .htaccess file.
When file is uploaded now it is time to check whether our website is GZIP is optimized visit one more time www.checkgzipcompression.com and enter URL of your website and make test.

If you get this result then your website is GZIP optimized and it will deliver content much faster to your valuable visitors!
Wordpress enable GZIP slide 2

I hope you this tutorial will help you to optimize your website. Thanks for visit www.it-tutorials.net and keep watching us as we always deliver new tutorials for you!

Bojan Markovic

Bojan Markovic

IT professional with almost 20 years of experience, mostly integrator of IT services in TV & Radio broadcasting and web based projects. Expert in broadcasting playout systems, video streaming services and SEO.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 + 7 =