The Vary: Accept-Encoding header is a response header used in HTTP to indicate that the content served to the client may vary based on the Accept-Encoding request header. This is particularly important for content compression, such as gzip or Brotli.
Adding this header ensures that:
- Proper Caching: Caches (like CDNs and browsers) store different versions of the resource for different encoding types.
- Avoiding Conflicts: Prevents issues where cached compressed resources are incorrectly served to clients that do not support compression.
Why Use the Vary: Accept-Encoding Header?
- Content Negotiation: Different clients (e.g., browsers) support different types of content encoding (gzip, Brotli, etc.). The Vary: Accept-Encoding header informs caches to store and serve the appropriate version.
- Caching Compatibility: Ensures that caches like CDNs or proxies do not mistakenly serve compressed content to clients that do not support it.
- SEO Benefits: Helps search engines index and cache the correct version of your resources.
In Apache
Modify the Apache configuration or .htaccess file.
- Open the .htaccess file in the root directory of your website.
- Add the following lines:
- Restart Apache to apply the changes:
<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
Header append Vary: Accept-Encoding
</IfModule>
sudo systemctl restart apache2
In Nginx
Modify the Nginx configuration file.
- Open your Nginx configuration file (e.g., /etc/nginx/nginx.conf or a site-specific file in /etc/nginx/sites-available/).
- Add the following line inside the server or location block:
- Save the file and reload Nginx:
gzip on;
gzip_vary on;
gzip_vary on;
sudo systemctl reload nginx
In cPanel
If you use cPanel:
- Log in to cPanel.
- Go to File Manager and open the .htaccess file in the root directory.
- Add the following:
<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
Header append Vary: Accept-Encoding
</IfModule>
Using PHP
If you cannot modify the server configuration, add the header in your PHP scripts.
- Add the following line at the top of your PHP script:
header("Vary: Accept-Encoding");
Verify the Vary Header
To confirm the header is applied:
- Use browser developer tools:
- Open Developer Tools in your browser (F12).
- Go to the Network tab.
- Check the response headers of your resource.
- Use command-line tools:
- Use online tools like:
curl -I https://yourdomain.com
Look for the Vary: Accept-Encoding header in the response.
Best Practices
- Enable Compression: Combine the Vary: Accept-Encoding header with gzip or Brotli compression for better performance.
- Avoid Overusing Vary: Only use Vary headers where necessary to prevent cache fragmentation.
- Test for Issues: Ensure the header is applied correctly and that all compressed/uncompressed versions work as expected.


