Skip to main content



How to Enable Gzip in .htaccess – A Complete Guide

How to Enable Gzip in .htaccess – A Complete Guide

If you want to speed up your website and improve your search engine rankings, one of the most effective techniques available to you is Gzip compression. When you enable Gzip htaccess configuration on your server, you can dramatically reduce the size of files sent from your server to your visitors’ browsers. The result is faster page load times, lower bandwidth consumption, and a significantly improved user experience. In this guide, we will walk you through everything you need to know about Gzip compression and how to enable it using your .htaccess file.

What Is Gzip Compression?

Gzip is a file compression format that has been widely used on the web for decades. When Gzip compression is enabled on a web server, it compresses files such as HTML, CSS, JavaScript, and XML before sending them to the browser. The browser then decompresses the files and renders the page as normal. This process happens automatically and is completely invisible to the end user.

The benefits of Gzip compression are substantial. In many cases, it can reduce the size of text-based files by up to 70–80%. This means that a page that might have taken several seconds to load can load much more quickly, which is particularly important for users on slower mobile connections.

Why Use .htaccess to Enable Gzip?

The .htaccess file is a powerful configuration file used by Apache web servers. It allows website owners and developers to make server-level changes without needing direct access to the main server configuration. If your website runs on an Apache server – which is the case for a large proportion of shared hosting environments – then the .htaccess file is your go-to tool for enabling Gzip compression.

Using .htaccess to enable Gzip htaccess rules is straightforward, requires no specialist server knowledge, and can be done in just a few minutes. It is one of the most commonly recommended performance optimisations for WordPress sites and other CMS platforms.

Before You Begin

Check Whether Gzip Is Already Enabled

Before making any changes, it is worth checking whether Gzip compression is already active on your server. You can do this by visiting a free online tool such as GiftOfSpeed or GTmetrix, both of which will tell you whether Gzip is currently enabled on your website. If it is not, you can proceed with the steps below.

Back Up Your .htaccess File

Before editing your .htaccess file, always make a backup. This file controls important aspects of how your server behaves, and an error in the syntax can cause your website to display errors or become temporarily inaccessible. Download a copy of your current .htaccess file and store it safely before making any changes.

How to Enable Gzip in .htaccess

There are two primary methods for enabling Gzip compression via .htaccess: using mod_deflate and using mod_gzip. The mod_deflate method is the more modern and widely supported approach, and it is what we recommend for most users.

Method 1: Using mod_deflate (Recommended)

The mod_deflate module is available on most Apache servers and is the standard way to enable Gzip compression. To use it, open your .htaccess file using a text editor or your hosting control panel’s file manager, and add the following code:

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

Save the file and upload it back to your server’s root directory if you edited it locally. Once saved, visit your website and run a compression test again to confirm that Gzip is now active.

Method 2: Using mod_gzip (Older Servers)

If your server uses an older version of Apache that does not support mod_deflate, you can try using mod_gzip instead. Add the following code to your .htaccess file:

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

This method is less commonly used today but can be a useful fallback if mod_deflate is not available on your server.

Troubleshooting Common Issues

500 Internal Server Error

If you see a 500 error after editing your .htaccess file, it is likely that there is a syntax error in the code or that the relevant module is not enabled on your server. Restore your backup file immediately and contact your hosting provider to confirm which modules are available.

Gzip Still Not Showing as Active

If your compression test still shows that Gzip is not enabled after making the changes, try clearing your browser cache and any caching plugins you may be using. It is also worth checking with your hosting provider, as some managed hosting environments handle Gzip at the server level and may override .htaccess settings.

Combining Gzip with Other Performance Optimisations

Enabling Gzip compression is just one piece of the website performance puzzle. For the best results, you should combine it with other optimisation techniques such as browser caching, image compression, minification of CSS and JavaScript files, and the use of a content delivery network (CDN). Together, these techniques can have a transformative effect on your website’s speed and performance scores.

For more in-depth guides on website performance, server configuration, and digital tools, visit the DA Manager blog, where you will find a wealth of practical resources to help you get the most from your website.

Final Thoughts

Learning how to enable Gzip htaccess configuration is one of the simplest yet most impactful steps you can take to improve your website’s performance. By adding just a few lines of code to your .htaccess file, you can reduce file sizes significantly, improve page load times, and deliver a better experience to every visitor who lands on your site. Whether you are running a small personal blog or a large e-commerce platform, Gzip compression is an optimisation that is well worth implementing today.