如何使用gzip的COM pression的.css和.js文件在我的网站上的文件? [英] How can I use gzip compression for .css and .js files on my websites?

查看:155
本文介绍了如何使用gzip的COM pression的.css和.js文件在我的网站上的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在辛苦劳作几天将获得gzip COM pression的网站我有我的共享托管服务器上工作。我已经成功了一定程度,但可惜的.css和.js文件都被排除在外,尽管我尽了最大努力。由于我使用共享的主机,我一定要在Apache配置文件没有访问,所以我使出用我的.htaccess文件来实现这一点。

I have been toiling for days to get gzip compression working on the websites I have on my shared hosting server. I've had a certain level of success but sadly .css and .js files are being left out, despite my best efforts. Since I'm using shared hosting, I have no access to the apache configuration file, so I have resorted to using my .htaccess file to achieve this.

我目前正在访问的网站是峰值热的,运行Word preSS,以下是.haccess文件我使用:

The site I'm currently working on is Peak Heat, running Wordpress, and below is the .haccess file I'm using:

# 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>

当我检查使用Firebug v1.8.3与谷歌的PageSpeed​​ V1.12网站,它表明了以下文件没有COM pressed:

When I check the site using Firebug v1.8.3 with Google PageSpeed v1.12, it shows that the following files haven't been compressed:

/wp-includes/js/jquery/jquery.js?ver=1.6.1

/wp-includes/js/jquery/jquery.js?ver=1.6.1

/wp-content/themes/peak-heat/script/global.js

/wp-content/themes/peak-heat/script/global.js

/wp-content/plugins/contact-form-7/jquery.form.js?ver=2.52

/wp-content/plugins/contact-form-7/jquery.form.js?ver=2.52

/wp-content/themes/peak-heat/style.css

/wp-content/themes/peak-heat/style.css

/wp-content/plugins/contact-form-7/scripts.js?ver=3.0

/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/contact-form-7/styles.css?ver=3.0

/wp-content/plugins/testimonials-widget/testimonials-widget.css

/wp-content/plugins/testimonials-widget/testimonials-widget.css

检查网站的网址本身带有在线GIDZipTest 网站,它证实了COM pression被启用,但是当我检查以上.css和.js文件,它说,他们不是COM pressed。

Checking the website URL itself with the online GIDZipTest site, it confirms that compression is enabled, but when I check the above .css and .js files, it says that they're not compressed.

我能做些什么,以包括所有的.css和.js文件时,COM pressing我的网站?

What can I do to include all .css and .js files when compressing my site?

推荐答案

AddOutputFilterByType DEFLATE文/ CSS 设置COM pression到放气,而不是的gzip ,所以使用 filesMatch 并设置内容-encoding X-放气

AddOutputFilterByType DEFLATE text/css sets the compression to deflate, not gzip, so use filesMatch and set the Content-Encoding header to x-deflate:

<filesMatch "\.(js|css)$">
Header set Content-Encoding x-deflate
# Header set Content-Encoding compress
# Header set Content-Encoding x-gzip
</filesMatch>

如果失败,取消注释 COM preSS 行和评论的 X-放气行。 gzip的不是标准的Apache安装,所以安装的 gzip的模块如果放气是不够的。作为最后的手段,创建gzip压缩版本的CSS和JS文件的。

If that fails, uncomment the compress line and comment the x-deflate line. gzip is not part of the standard Apache installation, so install the gzip module if deflate is not sufficient. As a last resort, create gzipped versions of your CSS and JS files.

这篇关于如何使用gzip的COM pression的.css和.js文件在我的网站上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆