我不知道或不了解如何做到这一点。谁能解释它是什么以及它做了什么?
答案 0 :(得分:18)
我在https://tools.pingdom.com/和https://developers.google.com/speed/pagespeed/insights/
获得了近100%的分数我找到了一篇有用的帖子来加速wordpress网站或博客https://www.keycdn.com/blog/speed-up-wordpress/
通过其他一些优化,我也在.htaccess
文件中使用我网站上的代码(通常隐藏在主网站文件夹中)
我的服务器是Apache,您可以登录托管控制面板(如cPanel / WHM Panel)(如果您的服务器是nginx,请检查keycdn.com帖子)
(复制并粘贴.htaccess文件中的代码,它对我有用)
(如果适合你的话,请回答这个答案)
<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>
答案 1 :(得分:16)
我认为您必须为css
,js
和xml
等特定文件启用压缩功能。
以下代码添加到域的根.htaccess文件中将在您的服务器上启用此类功能:
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
如果您想为此规则添加更多文件类型,只需将其扩展名添加到语句中即可! <FilesMatch "\.(js|css|xml|gz|newone)$">
答案 2 :(得分:3)
我也遇到了无效的问题
发生的事情是我的php文件还有另一个头文件指令
我有一个Header设置Cache-control - 它会覆盖Header追加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>
这不是我的实际Cache-Control语句 - 只是简化了示例代码。