Guides Guides 6 min read 1,167 words

Enable GZIP Compression: Apache .htaccess Guide

Enable Apache text compression with mod_deflate, check the delivered response, and troubleshoot safely without outdated browser exceptions.

To enable GZIP compression on Apache, configure mod_deflate to compress suitable response types, then verify that a request accepting gzip receives a Content-Encoding: gzip response. A short .htaccess rule can do this when your host allows the required directives.

We cover preparation, a minimal configuration, and checks that distinguish working compression from an ignored file. First check the existing response: if compression is already active, another configuration block may be unnecessary.

What GZIP compression changes

HTTP compression reduces the representation sent over the network. The client advertises supported encodings in Accept-Encoding, and the server identifies its chosen encoding in Content-Encoding. For text such as HTML, CSS, and JavaScript, compression can reduce the amount transferred.

The browser decodes the response before using it. This is not the same as rewriting your source code, resizing images, or caching a page. We recommend treating each performance mechanism separately so you can verify what a particular change accomplished.

Despite the module's name, Apache's mod_deflate produces the HTTP gzip encoding, not the HTTP deflate encoding. You should therefore look for gzip in the response header rather than expecting a header that matches the module name.

Step 1: check whether compression already works

Inspect a real page response and representative CSS or JavaScript responses, not only the homepage. A browser's network inspector can show response headers. From a terminal with curl available, the following example requests gzip and displays headers while discarding the downloaded body:

curl -sS -D - -o /dev/null -H 'Accept-Encoding: gzip' https://example.com/

Replace the example URL with your final HTTPS page URL. If you receive a redirect, check the destination separately; a redirect response is not the page body you meant to evaluate. Look for Content-Encoding: gzip on the successful content response.

A response using another supported encoding, such as Brotli (br), is also compressed. Do not disable a working compression arrangement simply to make a general testing tool display the word gzip. Record which resource and request headers produced the result before changing anything.

Online compression checkers can offer another view, but we suggest verifying the actual response headers yourself. A report about one URL does not establish that all your text resources are compressed.

GZIP response flow: a client accepts gzip, Apache compresses suitable content, the response identifies gzip encoding, and the browser decodes the body. No fixed size reduction is promised.

Step 2: confirm Apache support and back up the file

This configuration is for Apache. It requires mod_deflate and support for the AddOutputFilterByType directive. The hosting provider must also permit the relevant directory-level configuration. See our guide to .htaccess files before editing an unfamiliar installation.

Use your provider's supported file-access method to locate the document root for the website. Do not assume every hosting account uses the same directory name. Download or copy the existing .htaccess file to a safe location outside the public website before editing it.

Preserve application rules, redirects, and existing authorization settings. If the file does not exist, confirm that your host supports it before creating a plain-text file named exactly .htaccess. If you manage Apache directly, its documentation recommends using the main server configuration rather than directory overrides where possible.

Apache controls permitted overrides through AllowOverride and AllowOverrideList. A directory file cannot override a restriction that prevents Apache from reading or accepting it. Ask your host to confirm support rather than repeatedly adding alternative snippets.

Step 3: add a minimal text-compression rule

Add this block to the appropriate .htaccess file without deleting unrelated configuration:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>

This follows Apache's documented example for common text-based media types. It targets the response's content type rather than merely checking its filename. If another resource type needs compression, verify its actual content type and the host's configuration before extending the list.

The IfModule wrapper makes this block conditional on the module being loaded. It does not install the module. That also means saving the file without an error is not evidence that compression has started: the enclosed directive can be skipped if the module is unavailable.

Save the edited file, or upload your edited copy using the provider's supported method. Directory-level configuration is read as requests are handled, but the hosting environment and any intermediary caches still matter when evaluating the delivered response.

Step 4: verify the result and keep a rollback path

Repeat the gzip request against your final HTML URL, then repeat it for a real CSS or JavaScript URL. Confirm the content still loads correctly and inspect Content-Encoding. Apache's module also supplies Vary: Accept-Encoding so caches can distinguish representations for clients with different encoding support.

If you use special exclusions based on other request headers, Apache says those dependencies must also be represented in Vary. The minimal example above does not include user-agent exclusions. We would not add old browser-matching rules or a Vary: User-Agent header without a current requirement for them.

If a content cache is involved, use its documented invalidation method before concluding that the origin configuration failed. Compare the response actually delivered to visitors, and avoid stacking several compression solutions without understanding which layer produces the encoding.

Keep your backup until ordinary pages and important application flows behave normally. If the change introduces a server error, restore the previous file and read the Apache error log or give the host the error message. Do not leave a broken site online while trying random directive combinations.

Why compression may still be missing

  • The module is unavailable. The conditional block may be skipped; your host must provide the required capability.
  • The file or directives are not being used. Check the document root and permitted overrides.
  • The content type does not match. Inspect the actual response before changing the filter list.
  • You checked a different response. Redirects, cached content, or a proxy can complicate a single-URL test.
  • The resource is already compressed. Another content encoding may be present instead of gzip.

Do not apply text compression indiscriminately to images, audio, video, or archives that are already compressed. MDN notes that compressing compressed formats again can be counterproductive. Also review Apache's warning about BREACH-style information-disclosure risks for some applications combining compression and TLS; sensitive dynamic responses deserve an application-specific security review.

For related work, read how web caching differs from compression and our Cloudflare compression guide. If you first need to untangle domain and hosting accounts, our WHOIS lookup can help investigate registration details. Confirm the web-server configuration with the hosting provider separately; GZIP is not a domain-registration setting.

Frequently asked questions

Does this work on every hosting server?

No. These are Apache directives with specific module and override requirements. Use your provider's documentation for a different server or managed platform.

Does GZIP replace caching?

No. Compression changes the transferred representation. Caching is a separate mechanism; a site may use both, but enabling one does not establish that the other is configured correctly.

Is a particular speed improvement guaranteed?

No. We recommend checking transfer behavior and measuring your actual pages rather than promising a fixed reduction in load time or a search-ranking benefit.

Sources