How to force gzip compression in Apache with .htaccess

Apache: How to force gzip compression in Apache with .htaccess

You can force Apache to compress data before sending it by adding a few lines of code to your .htaccess file.

  1. <ifModule mod_gzip.c>
  2.     mod_gzip_on Yes
  3.     mod_gzip_dechunk Yes
  4.  
  5.     mod_gzip_item_include file .(html?|txt|css|js|php)$
  6.     mod_gzip_item_include mime ^application/x-javascript.*
  7.     mod_gzip_item_include mime ^text/.*
  8.     mod_gzip_item_exclude mime ^image/.*
  9.  
  10.     mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  11. <\/ifModule>

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to force gzip compression in Apache with .htaccess

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Apache language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to check if two strings are anagrams in PHP?

Push vs Pop vs Shift vs Unshift

What is Agile software development?

How to exclude URLs from the Laravel CSRF protection?

How to replace whitespace with a single space

How to Extract Query String (from a URL) Into an Associative Array in PHP

How to increment a value in the database

How to find the intersection (same values) from two arrays?

How to hide directory listing in .htaccess?

How to set environmental variables in your .htaccess file