Html 如何指定 Vary: Accept-Encoding 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16642190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:34:01  来源:igfitidea点击:

How to Specify a Vary: Accept-Encoding header?

htmlcss.htaccessseogzip

提问by jshariar

Google and pingdom.com says i should "Specify a Vary: Accept-Encoding header"

谷歌和 pingdom.com 说我应该“指定一个变量:接受编码头”

i do not know or understand how to do this. Can anyone explain what it is and what it does?

我不知道或不明白如何做到这一点。谁能解释一下它是什么以及它的作用是什么?

回答by BlackFire

I am getting nearly 100% score in https://tools.pingdom.com/and https://developers.google.com/speed/pagespeed/insights/

我在https://tools.pingdom.com/https://developers.google.com/speed/pagespeed/insights/ 中获得了近 100% 的分数

I found a helpful post to speed up wordpress website or blog https://www.keycdn.com/blog/speed-up-wordpress/

我发现了一个有用的帖子来加速 wordpress 网站或博客https://www.keycdn.com/blog/speed-up-wordpress/

With some other optimizations, I am also using below code on my site in .htaccessfile (usually hidden in main website folder)

通过其他一些优化,我还在我的网站上的.htaccess文件中使用以下代码(通常隐藏在主网站文件夹中)

My Server is Apache, You can check in hosting control panel (Like cPanel/WHM Panel)(if your server is nginx check keycdn.com post)

我的服务器是 Apache,您可以查看托管控制面板(如 cPanel/WHM 面板)(如果您的服务器是 nginx,请查看 keycdn.com 帖子)

(Copy and paste below code in .htaccess file, it's working good for me)

(将下面的代码复制并粘贴到 .htaccess 文件中,它对我很有用)

(Upvote this answer if works for you)

(如果对您有用,请支持此答案)

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType image/svg "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/xhtml+xml "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 1 month"
</IfModule>

<ifModule mod_headers.c>
  <filesMatch ".(css|jpg|jpeg|png|gif|swf|svg|js|ico)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch ".(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/atom+xml
  AddOutputFilterByType DEFLATE application/rdf+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-woff
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/truetype
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>

回答by Patartics Milán

I think you have to enable compression for specific files like css, jsand xml. The following code added to your domain's root .htaccess file will enable this kind of feature on you server:

我认为您必须为特定文件启用压缩,例如css,jsxml. 添加到您域的根 .htaccess 文件中的以下代码将在您的服务器上启用此类功能:

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

If you would like to add more file type to this rule, just simple add the its extension to the statement! <FilesMatch "\.(js|css|xml|gz|newone)$">

如果您想向此规则添加更多文件类型,只需将其扩展名添加到语句中即可! <FilesMatch "\.(js|css|xml|gz|newone)$">

回答by Nat

I also had problems with this not working

我也遇到了这个不起作用的问题

Wht happened was I had another header directive for my php files

发生的事情是我的 php 文件有另一个标头指令

I had a Header set Cache-control - and it overwrites the Header append Vary, so you have to put them in the same block.
What I had to do was set the Vary for all the other files in one Filesmatch statement and the Cache and Vary for the php files in a separate FilesMatch statement like this:

我有一个 Header set Cache-control - 它覆盖了 Header append Vary,所以你必须把它们放在同一个块中。
我必须做的是在一个 Filesmatch 语句中为所有其他文件设置 Vary,在单独的 FilesMatch 语句中为 php 文件设置 Cache 和 Vary,如下所示:

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


<IfModule mod_headers.c>
<FilesMatch "\.(php)$">
 Header set Cache-Control "max-age=300"
 Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>

That's not my actual Cache-Control statement - just simplified for the example code.

那不是我实际的 Cache-Control 语句 - 只是针对示例代码进行了简化。