How to enable mod_deflate on Apache 2.4?

Compressing data like html, js, css before transmitting them to the visitors can speed up your sites and save you bandwidth. Recently I have updated Apache on one server from version 2.2 to 2.4 but my mod_deflate rules won’t work any more (because of changes in the new version of Apache).

Here are my old mod_deflate rules I used on Apache 2.2:

<IfModule mod_deflate.c>
<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 filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>

<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>
</IfModule>

And here’s my new Apache 2.4 code that enables mod_deflate compression:

<IfModule mod_deflate.c> 
SetOutputFilter DEFLATE 
SetEnvIfNoCase Request_URI \.(?:gif|jpg|png|ico|zip|gz|mp4|flv)$ no-gzip 
</IfModule>

Nice and easy! You can add other extensions you do not want to compress to the list above and that should be it!

cPanel/WHM users should login to WHM then go to Service Configuration -> Apache Configuration -> Include Editor then locate Pre VirtualHost Include on the page and select All Versions from the list (or current version of the Apache in my case 2.4.10).

Then simply copy paste the code above into the box, save and restart the Apache service.

Note: The process is same for Apache 2.2 just use the appropriate code from above.

View Comments

  • I tried adding the code to .htaccess but it didn't work but when I added the same to Apache Configuration Globally then it worked.

    What can be the reason?

    • That's odd. Have you verified that mod_deflate is actually working once it's placed in Apache Configuration Globally?

Share
Published by
Nick

Recent Posts

How to Manage Storage on Smartphone: 12 Ways

Can't download new apps to your phone because there isn't enough storage space? Lack of…

10 months ago

Spotlight Search won’t open the files it finds in Dropbox with associated application

This issue started to appear on macOS 13 Ventura after recent Dropbox update. I would…

1 year ago

Windows 10 Search fix

Since this morning (Feb 5th 2020) search just stopped working on my Windows 10 (version…

4 years ago

The mysql_result in mysqli

Many of you have run into a problem when you were working on some old…

4 years ago

How to convert physical Windows PC into a virtual machine

After 10 years of running my main desktop computer on Windows 7 - time has…

4 years ago

How to downgrade PHP 7.4 to PHP 7.3 on VestaCP running on CentOS 7

I installed VestaCP for a clien on a new dedicated server running CentOS 7. Once…

5 years ago