How to specify "Vary: Accept-Encoding" header in .htaccess

Google PageSpeed ​​says I need to “Define an Accept: Encoding header for JS and CSS. How do I do this in .htaccess?

+74
.htaccess pagespeed
Sep 04 '10 at 6:48
source share
7 answers

I assume that this means that you enable gzip compression for your css and js files, as this will allow the client to receive both gzip-encoded content and simple content.

Here's how to do it in apache2:

<IfModule mod_deflate.c> #The following line is enough for .js and .css AddOutputFilter DEFLATE js css #The following line also enables compression by file content type, for the following list of Content-Type:s AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml #The following lines are to avoid bugs with some browsers BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html </IfModule> 

Here's how to add the Vary Accept-Encoding header: [src]

 <IfModule mod_headers.c> <FilesMatch "\.(js|css|xml|gz)$"> Header append Vary: Accept-Encoding </FilesMatch> </IfModule> 

Vary: Header Vary: Indicates that the content served by this URL will change depending on the value of a particular request header. It says here that it will serve different content for clients that say they are Accept-Encoding: gzip, deflate (request header) than the content served to clients who do not send this header. The main advantage of this, AFAIK, is to let intermediate cache proxies know that they must have two different versions of the same URL due to such a change.

+81
Sep 04 '10 at 6:51
source share

I am afraid that Aularon did not give sufficient steps to complete this process. With a little trial and error, I was able to successfully enable gzipping on my dedicated WHM server.

Following are the steps:

  • Launch EasyApache in WHM, select Deflate in the Comprehensive Settings list, and rebuild the server.

  • After that, go to Services configuration → Apache configuration → Turn on the editor → Install VirtualHost Include, select “All versions”, and then paste the code mod_headers.c and mod_headers.c (indicated above in the Aularon message) at the top on the other in the input field.

  • After saving, I saw an average data saving of 75.36%! You can run before and after the test using this HTTP compression tool to see your own results: http://www.whatsmyip.org/http_compression/

Hope this works for all of you!

  • Matt
+4
Aug 18 2018-11-18T00:
source share

To also deploy font files!

 add "x-font/otf x-font/ttf x-font/eot" 

how in:

 AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml x-font/otf x-font/ttf x-font/eot 
+3
Aug 01 '12 at 18:21
source share

Many hours spent to clarify what it was. Please read this post to get the .HTACCESS extended codes and what they do.

You can use:

 Header append Vary "Accept-Encoding" #or Header set Vary "Accept-Encoding" 
+2
Aug 22 '16 at 19:33
source share

It drove me crazy, but it seems that aularon edit was missing a colon after "Vary" . So changing "Vary Accept-Encoding" to "Vary: Accept-Encoding" fixed the problem for me.

I would comment on the posts below, but it seems like this will not allow me.

In any case, I hope that this will save the same troubles that I had.

+1
Jun 21 2018-12-12T00:
source share

It is not necessary to indicate or even check whether the file / is compressed, you can send it to each file, for each request.

It tells downstream proxies how to match future request headers to decide whether a cached response can be used, rather than requesting a fresh one from the source server.

 <ifModule mod_headers.c> Header unset Vary Header set Vary "Accept-Encoding, X-HTTP-Method-Override, X-Forwarded-For, Remote-Address, X-Real-IP, X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port, X-Forwarded-Server" </ifModule> 
  • unset is fixing some bugs on earlier GoDaddy hosting, optional.
0
Jan 13 '16 at 4:30
source share

if someone needs this for the NGINX configuration file, here is a snippet:

 location ~* \.(js|css|xml|gz)$ { add_header Vary "Accept-Encoding"; (... other headers or rules ...) } 
0
Aug 17 '16 at 8:09
source share