Bobcares

How we enable gzip compression through htaccess?

by | Mar 25, 2020

Looking for a way to speed up your Apache-based web server?  One option is to enable gzip compression through the .htaccess file.

Gzip compressions reduce file size that in-turn increases website speed.

That is why our Support Engineers enable gzip compression in all web servers, as a part of our Server Management Services.

To know how to optimize the website loading time using the .htaccess file, read on.

 

What is gzip compression?

Gzip is a method that makes files smaller in size, that is, it simply compresses the files. But does compressing files helps in increasing website speed? Yes. Let’s see how it is possible.

When a visitor makes a request to access a website, the server sends the corresponding file. If the file is larger, say 1MB, it takes around 5 seconds to reach the visitor’s browser.

At the same time, if the server compresses this 1MB file to 50KB using a compression method like gzip, it reaches its destination within milliseconds.

gzip compression htaccess

That is, smaller files have a better transmission in a network.

Hence, compression improves the speed of the website and reduces the load time. Because the visitor will need to download smaller web files when compared to the original ones.

Next, let’s get into more detail on how to enable gzip compression in an Apache-based web server using a .htaccess file.

 

How to enable gzip compression using .htaccess?

The .htaccess file is nothing but the configuration file in Apache-based web servers.

We, the webmasters actually use two different mods to enable the gzip compression: mod_gzip and mod_deflate.

Mod_gzip enables gzip compression and mod_deflate compresses the output from the server before it is being sent to the visitor.

So, both do the same thing. Generally, we recommend using mod_deflate since it is easier to use.

We can enable these by adding a code in the .htaccess file.

To enable mod_deflate, our Support Engineers add the below code in .htaccess file

<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 we add the below code to enable mod_gzip,

<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>

 

Common errors related to gzip compression

Recently, one of our customers approached us telling us that, the gzip compression was not working. He received the below error in the plugin.

Gzip is not working properly:

He enabled deflate gzip using the .htaccess file.

Let’s see how our Support Engineers fix this problem.

 

1. Checked permissions

Our Support Engineers started troubleshooting the problem by checking the permission of .htaccess file.

On checking, we found that the permission of the file was set to 644 which was correct. So we moved on to troubleshoot further.

 

2. Checked extension availability

Then, we further accessed the php.ini file to see if the zlib extension is set or not. We saw it was not active. So we added the below code in php.ini.

zlib.output_compression = On

Sometimes this may fix the error. But in most cases, it won’t. So we checked further.

 

3. Checked Apache module installation

Sometimes, the mod_gzip or mod_deflate modules will not be set on the server which causes errors. So, we checked for the availability of the module using the below command:

httpd -M | grep deflate

If mod_deflate is available, we can see deflate_module (shared).

Since we could not see the module in the server, we enabled it.

Finally, this fixed the error.

 

[Need any assistance in setting the gzip compression and fixing its errors? – We’ll help you.]

 

Conclusion

In short, the gzip compression helps in reducing the file size and making the website load faster. Today, we saw how our Support Engineers enable gzip compression using htaccess and fix its errors.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. stref

    Hello how to add expires i am try to add this
    ## EXPIRES CACHING ##

    ExpiresActive On
    ExpiresByType image/jpg “access plus 1 year”
    ExpiresByType image/jpeg “access plus 1 year”
    ExpiresByType image/gif “access plus 1 year”
    ExpiresByType image/png “access plus 1 year”
    ExpiresByType text/css “access plus 1 month”
    ExpiresByType application/pdf “access plus 1 month”
    ExpiresByType text/x-javascript “access plus 1 month”
    ExpiresByType application/x-shockwave-flash “access plus 1 month”
    ExpiresByType image/x-icon “access plus 1 year”
    ExpiresDefault “access plus 2 days”

    ## EXPIRES CACHING ##

    OR
    # 1 year

    Header set Cache-Control “max-age=29030400, public”

    # 1 Week

    Header set Cache-Control “max-age=604800, public”

    # 2 Days

    Header set Cache-Control “max-age=172800, proxy-revalidate”

    # 1 Minute

    Header set Cache-Control “max-age=60, private, proxy-revalidate”

    And all is slower with your tutorials is perfect

    Reply
    • Hiba Razak

      Hi,
      Please contact our support through live chat(click on the icon at right-bottom).

      Reply

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.