我已经辛苦工作了几天,以便在我的共享托管服务器上的网站上运行gzip压缩。我已经取得了一定程度的成功,但遗憾的是.css和.js文件被遗漏了,尽管我付出了最大的努力。由于我正在使用共享主机,因此我无法访问apache配置文件,所以我已经使用我的.htaccess文件来实现此目的。
我目前正在处理的网站是Peak Heat,运行Wordpress,以下是我正在使用的.haccess文件:
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/js
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 week"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
当我使用带有Google PageSpeed v1.12的Firebug v1.8.3检查网站时,它显示以下文件尚未压缩:
/wp-includes/js/jquery/jquery.js?ver=1.6.1
/wp-content/themes/peak-heat/script/global.js
/wp-content/plugins/contact-form-7/jquery.form.js?ver=2.52
/wp-content/themes/peak-heat/style.css
/wp-content/plugins/contact-form-7/scripts.js?ver=3.0
/wp-content/plugins/contact-form-7/styles.css?ver=3.0
/wp-content/plugins/testimonials-widget/testimonials-widget.css
使用online GIDZipTest网站检查网站网址本身,确认已启用压缩,但当我检查上述.css和.js文件时,表示它们未被压缩。
压缩网站时,如何包含所有.css和.js文件?
答案 0 :(得分:12)
AddOutputFilterByType DEFLATE text/css
将压缩设置为deflate
,而不是gzip
,因此请使用filesMatch
并将Content-Encoding
标头设置为x-deflate
:
<filesMatch "\.(js|css)$">
Header set Content-Encoding x-deflate
# Header set Content-Encoding compress
# Header set Content-Encoding x-gzip
</filesMatch>
如果失败,请取消注释compress
行并对x-deflate
行发表评论。 gzip不是标准Apache安装的一部分,因此如果deflate不够,请安装gzip module。作为最后的手段,create gzipped versions of your CSS and JS files。
答案 1 :(得分:1)
我认为你错过了AddOutputFilterByType DEFLATE text/javascript
。添加它可以解决我的问题。