Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

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


# 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

And we add the below code to enable mod_gzip,


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$

 

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 *

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF