【问题标题】:I want to enable g zip compression in php website i have found two way to do it which one more efficient我想在 php 网站中启用 g zip 压缩我找到了两种方法来做到这一点,其中一种更有效
【发布时间】:2016-09-07 11:00:45
【问题描述】:

我有两种方法可以做到这一点,下面给出了一种更有效的方法以及我需要放置的位置。 htaccess 或其他地方

方法一:

# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>

# Expires Headers - 2678400s = 31 days
<ifmodule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7200 seconds"
  ExpiresByType image/gif "access plus 2678400 seconds"
  ExpiresByType image/jpeg "access plus 2678400 seconds"
  ExpiresByType image/png "access plus 2678400 seconds"
  ExpiresByType text/css "access plus 518400 seconds"
  ExpiresByType text/javascript "access plus 2678400 seconds"
  ExpiresByType application/x-javascript "access plus 2678400 seconds"
</ifmodule>

# Cache Headers
<ifmodule mod_headers.c>
  # Cache specified files for 31 days
  <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
  Header set Cache-Control "max-age=2678400, public"
  </filesmatch>
  # Cache HTML files for a couple hours
  <filesmatch "\.(html|htm)$">
  Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  # Cache PDFs for a day
  <filesmatch "\.(pdf)$">
  Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  # Cache Javascripts for 31 days
  <filesmatch "\.(js)$">
  Header set Cache-Control "max-age=2678400, private"
  </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>

【问题讨论】:

  • 如果你想启用gzip你需要使用方法2,方法一是启用deflate(zip)。

标签: php gzip


【解决方案1】:

启用 gzip 压缩的最简单方法是在 htaccess 中添加代码。如果您有权访问服务器根目录并了解系统设置,则使用 httpd-vhosts.conf 文件启用 gzip 压缩。

【讨论】:

  • 但是我需要添加哪些代码我有两种方法,在描述标签中指定了一种更好
  • 第二种方法仅用于启用 gzip 压缩。第一种方法启用压缩并为静态文件设置缓存到期时间。如果您已经在任何其他步骤中设置了到期时间,则使用第二种方法,否则使用第一种方法。
  • 如果我在 httpd-vhosts.conf 中添加代码是否比 htacess 快
  • 第二种方法也有 和第一种方法有 所以我有面团怎么能一样如果有什么需要添加或删除,请检查方法一个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
相关资源
最近更新 更多