Adding the Vary: Accept-Encoding header in Apache ensures that the server informs caching systems (browsers, CDNs, proxies) to store and serve different versions of a resource based on whether compression is used.
Here is how to modify Apache to specify the Vary: Accept-Encoding header.
Enable Required Apache Modules
Ensure the following Apache modules are enabled:
- mod_headers: To modify HTTP headers.
- mod_deflate: To enable gzip compression (optional but recommended).
Run the following commands to enable these modules:
sudo a2enmod headers
sudo a2enmod deflate
sudo systemctl restart apache2
sudo a2enmod deflate
sudo systemctl restart apache2
Add Vary: Accept-Encoding Header
Option 1: Add to the Apache Configuration
- Open the main Apache configuration file:
- Add the following directive to specify the Vary: Accept-Encoding header:
- If gzip compression is enabled, ensure it works with Vary:
- Save the file and exit.
sudo nano /etc/apache2/apache2.conf
or, if you are working with virtual hosts:
sudo nano /etc/apache2/sites-available/your-site.conf
<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
Header append Vary: Accept-Encoding
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary: Accept-Encoding
</IfModule>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary: Accept-Encoding
</IfModule>
Option 2: Use .htaccess File
If you do not have access to the server configuration files, you can modify the .htaccess file in your website root directory.
- Open or create a .htaccess file:
- Add the following lines:
- Save and close the file.
nano /var/www/html/.htaccess
<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary: Accept-Encoding
</IfModule>
Header append Vary: Accept-Encoding
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary: Accept-Encoding
</IfModule>
Restart Apache
After making changes, restart Apache to apply the new configuration:
sudo systemctl restart apache2
Verify the Vary: Accept-Encoding Header
- Using cURL: Run the following command:
- Using Browser Developer Tools:
- Open your browser Developer Tools (F12).
- Go to the Network tab.
- Reload your website and inspect the response headers.
- Online Tools: Use tools like WebPageTest or GTmetrix to confirm the header is applied.
curl -I https://yourdomain.com
Check the output for:
Vary: Accept-Encoding
Optional: Enable Compression for Better Results
- Ensure gzip or Brotli compression is enabled for your website.
- Example configuration for gzip in Apache:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
</IfModule>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
</IfModule>
Best Practices
- Combine Vary: Accept-Encoding with proper cache-control headers for optimal caching.
- Test regularly to ensure the header is applied correctly.
- Avoid excessive Vary headers (e.g., Vary: Accept-Encoding, Accept-Language, User-Agent), as they may fragment the cache.


