如何在.htaccess中指定"Vary:Accept-Encoding"标头

Sta*_*bie 81 .htaccess pagespeed

Google PageSpeed说我应该为JS和CSS"指定一个Vary:Accept-Encoding标头".我怎么在.htaccess中这样做?

aul*_*ron 89

我想这意味着你为你的css和js文件启用gzip压缩,因为这将使客户端能够接收gzip编码的内容和普通的内容.

这是在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>
Run Code Online (Sandbox Code Playgroud)

以下是添加Vary Accept-Encoding标题的方法:[src]

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

Vary:报头通知该内容提供该URL将按照一定的请求头的值而变化.在这里,它表示它将为那些说出它们Accept-Encoding: gzip, deflate(请求标题)的客户提供不同的内容,而不是为不发送此标头的客户端提供的内容.这个AFAIK的主要优点是让中间缓存代理知道他们需要有相同网址的两个不同版本,因为这样的改变.

  • 我认为mod_deflate是[假设](http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#proxies)默认发送Vary标头. (3认同)
  • Apache 2.2在上面的答案中不需要mod_headers部分.mod_deflate已经做了你需要的.http://httpd.apache.org/docs/2.2/mod/mod_deflate.html (3认同)