How to Use GZip Compression Inside .htaccess File

GZip compression helps all assets send to the client as a compressed file. This will help a web site to be open faster.

shape
shape
shape
shape
shape
shape
shape
shape
How to Use GZip Compression Inside .htaccess File

How to Use GZip Compression Inside .htaccess File

Gzip is a widely-used method for file compression on the web. When a user visits a website, their browser sends a request to the server for the requested resources, such as HTML, CSS, and JavaScript files. If these files are large, it can take a long time for them to be transferred over the network, resulting in slow page load times.

One way to improve the speed of a website is to use gzip compression on the server. When a file is compressed with gzip, it is reduced in size, which means it can be transferred to the browser more quickly. This can lead to significant improvements in page load times, especially for users with slower internet connections.

To enable gzip compression on a website, it needs to be configured on the server. This is typically done by editing the server's configuration file and adding the necessary code to enable gzip compression. The specific steps will depend on the type of server and the software being used. For example, in Apache server, you can enable gzip compression by adding the following code to the .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


It's also possible to use a Content Delivery Network (CDN) such as Cloudflare, which will automatically compress your website's files using gzip.

In conclusion, gzip compression is a simple and effective way to speed up a website. By reducing the size of the files that need to be transferred over the network, gzip compression can significantly improve page load times and provide a better experience for users.

The code below can also be used as a simple standart for you .htaccess file for compression if you don't need to compress everyting.

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


AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Write a comment