How to Add Vary: Accept-Encoding HTTP Header with .htaccess

Its a good idea to set Vary: Accept-Encoding HTTP header response from your server, especially if you display different websites for various user agents and even to define if the caching tool should serve the gzip compressed file or not.

First use the HTTP headers check tool to see the headers sent by your server. Do you see the Vary headers? If not then here is how to add it.

Add Vary: Accept-Encoding

vary accept encoding

You need to add the following code below to the .htaccess file. This file resides on the server root and can be accessed via FTP. [Note it is a very powerful file and the wrong configuration can take your site down. Always keep a .htaccess backup, just in case you need to replace the original file.]

I have been trying various combinations, and this code finally seems to work for our SSD hosting server. Add this code below

<IfModule mod_headers.c>
Header set Vary "Accept-Encoding"

Now again test your site headers with the HTTP headers tool. Now the Vary header should appear.

Why Vary HTTP Header

If you read the Google  guide to developing smartphone-optimized websites,  you will note it helps Googlebot discover your mobile-optimized content faster!  The Vary header can also be used to easily guide caching servers if they should serve the page from cache or not by varying the user agent.

Using Gzip? Accept-Encoding value can help if you use the gzip compression of web resources to increase site speed. Now, most websites use gzip compression as it allows faster loading of web pages (most WordPress caching plugins do gzip compression automatically). But this Vary Accept-Encoding header can help browser caching services understand if it needs to load the gzip version or not.

The first time I read about the vary header was when trying to implement Pagespeed on our server. I learned that PageSpeed will always respect Vary: Accept-Encoding and Vary headers on HTML files, and it is a great way to speed up your website by defining the variable.

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.