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.


  1. Konveyör bant says:

    htaccess good joob but little know spec maybe wizard and other tools use and creat htaccess site active and protetect this..

  2. Mary Gravitt says:

    Great tips! Here’s my idea regarding to that problem. I hope it could help you for the other solution way. Open your .htaccess file and add the below code and you are done. Read the example below.

    For Apache/.htaccess :
    [php]# BEGIN Specify a Vary: Accept-Encoding

    Header append Vary: Accept-Encoding

    # END Specify a Vary: Accept-Encoding[/php]

    For Nginx :
    [php]gzip_vary on[/php]

    For IIS :


  3. local contractors says:

    if you are using nginx or your provider is using it, this tip doesnt work

  4. Rashed Khan says:

    Fantastic tips! Here’s my own idea regarding compared to that problem. I desire it can help you for one other solution approach. Open the. htaccess record and increase the under code and you also are completed. Read the particular example under. For Apache/. htaccess:
    I think it’s very helpful for every parson. so try to read this article. many thanks for share this idea.

Leave a Reply

Your email address will not be published. Required fields are marked *