How to enable compression on your site
How to turn on text compression for your site, and how to check it is actually being applied afterwards.
HTML, CSS and JavaScript are all plain text, and plain text compresses extremely well — repeated words, whitespace and predictable patterns are exactly what compression algorithms are built to squeeze out. Turning on server-side compression means the server sends a compressed version of these files and the browser decompresses it on arrival, which is effectively free: the browser has to unpack the file anyway, and unpacking is far quicker than the time saved not transferring the uncompressed bytes.
Nearly every modern web server supports this, and on most hosting it is either already on or a short configuration change away.
Gzip and Brotli, and the difference between them
| Method | Support | Notes |
|---|---|---|
| Gzip | Universal — every browser in use today supports it | The safe default. Has been standard on the web for decades. |
| Brotli | Supported by all current major browsers | A newer algorithm that generally compresses text more effectively than Gzip at an equivalent setting. Falls back to Gzip automatically for the rare browser that does not support it, provided the server is configured to offer both. |
Neither of these compresses images, video or already-compressed files such as ZIP archives — compressing something that is already compressed usually makes it slightly larger, not smaller, and both Gzip and Brotli are configured to skip those file types automatically. This is purely for text: HTML, CSS, JavaScript, JSON, XML and similar.
How to check what is already running
Before changing anything, find out whether compression is already active. In your browser's developer tools, open the network panel, reload the page, click any HTML, CSS or JS request, and look at the response headers for Content-Encoding. A value of br means Brotli is active; gzip means Gzip is active; if the header is missing entirely, nothing is being compressed for that file.
Some setups compress HTML but not the CSS and JavaScript served alongside it, particularly where static files are handled by a different part of the server configuration. Check a stylesheet and a script file, not just the page itself.
Enabling it with .htaccess
On Apache-based hosting, compression is usually configured through the mod_deflate module (for Gzip) or mod_brotli where it is available, added to your site's .htaccess file. A typical Gzip configuration looks like this:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
The <IfModule> wrapper matters: it means the block is safely ignored if the module is not present, rather than causing an error. If Brotli is available on your hosting, it typically takes a similar block using mod_brotli, or may already be applied automatically at the server level without any .htaccess change needed — check your control panel's documentation or ask support before assuming you need to add it yourself. See how to use .htaccess for the general rules on editing this file safely.
A syntax error in .htaccess can take the entire site down with a server error, not just fail to apply the change you intended. Keep a copy of the working file before editing, so you can restore it immediately if something goes wrong.
If compression is already handled elsewhere
Many hosting platforms enable compression at the web server level rather than expecting every site to configure it individually through .htaccess. If your checks above already show Content-Encoding: gzip or br without you having changed anything, it is already on — adding a duplicate .htaccess rule on top does not compress it twice and is not needed. If in doubt, check with support before adding your own configuration, since two conflicting compression setups is a more likely source of problems than a slightly less optimal single one.
What compression will and will not fix
Compression reduces the number of bytes travelling over the network for text-based files — it has no effect on how many separate requests the page makes, how large its images are, or how long the server takes to generate a page in the first place. It is a genuinely worthwhile setting to have correct, and it is also one of the smaller levers in this section compared with image compression and reducing the number of requests a page makes, both of which typically account for a larger share of total page weight on an average site.
Confirming it worked
-
Reload the page with developer tools open
Check the network panel is actively recording before you reload, not opened afterwards.
-
Inspect the response headers on a few different file types
The page itself, a stylesheet, and a JavaScript file — not just one of the three.
-
Look for the Content-Encoding header
brorgzipconfirms it is active; its absence means that file is being served uncompressed. -
Compare transferred size against resource size
Most network panels show both figures side by side — a large gap between them confirms compression is doing real work.
Once compression and caching headers are both confirmed working, the next practical steps are usually minifying CSS and JavaScript to reduce the size of the files before they are even compressed, and setting browser caching headers so repeat visitors do not re-download them at all.
Related reading
How to tell a browser what it can keep and for how long, so a repeat visit does not re-download everything.
How to minify CSS and JavaScriptHow minification actually reduces file size, and how to test for the breakage it occasionally causes.
How to use .htaccess on your hosting accountOne file that controls redirects, rewrites and access rules for a whole folder — and one typo in it that can take the site offline.
What is server response time, and what is good?What the server is actually doing during that measurement, and how it differs from the total time a page takes to load.