简体   繁体   English

Gzip压缩和利用浏览器缓存不适用于我的商店

[英]Gzip Compression and leverage Browser Cache Not working for my store

I am trying to optimize my store, I have added Gzip Code and Leverage Browser Cache Code in my .htaccess File: 我正在尝试优化我的商店,我在.htaccess文件中添加了Gzip代码和杠杆浏览器缓存代码:

# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #

# Leverage browser caching using mod_headers #
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
        Header set Cache-Control "public"
    </FilesMatch>
</IfModule>
# End of Leverage browser caching using mod_headers #

Enable Apache mod_headers and mod_expires modules 启用Apache mod_headers和mod_expires模块

Please check mod_expires and mod_headers are enable or not on your store through the below code. 请通过以下代码检查mod_expires和mod_headers是否在您的商店中启用。

<?php phpinfo();?>

If both extension not unable to your server,please follow following steps:- 如果两个扩展程序都无法访问您的服务器,请按照以下步骤操作: -

Step 1 :- 步骤1 :-

Now login to server using SSH console and continue below steps: 现在使用SSH控制台登录服务器并继续以下步骤:

To enable mod_headers: 要启用mod_headers:

sudo a2enmod headers

To enable mod_expires: 要启用mod_expires:

sudo a2enmod expires

Step 2 :- 第2步 :-

after completing the server update, you need to restart Apache server to make these changes effective. 完成服务器更新后,您需要重新启动Apache服务器才能使这些更改生效。 Enter below line in your SSH console to restart Apache. 在SSH控制台中输入以下行以重新启动Apache。

service apache2 restart

Or else reboot your server by following code in SSH: 或者通过SSH中的代码重启服务器:

reboot

Use following code in your .htaccess file. 在.htaccess文件中使用以下代码。

<IfModule mod_headers.c>
     # YEAR
     <FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
          Header set Cache-Control "max-age=29030400"
     </FilesMatch>
     # WEEK
     <FilesMatch "\.(js|css|swf|woff)$">
         Header set Cache-Control "max-age=604800"
     </FilesMatch>
     # 45 MIN
     <FilesMatch "\.(html|htm|txt)$">
        Header set Cache-Control "max-age=86400"
     </FilesMatch>

     Header set Connection keep-alive

</IfModule>

<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl|asp|html)$
    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>

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

After clear your store cache and reload your website URL,recheck gtmetrix tool or whatever you are using tool. 清除商店缓存并重新加载网站网址后,请重新检查gtmetrix工具或使用工具的任何内容。

Here is the code for GZIP compression 这是GZIP压缩的代码

<ifModule mod_deflate.c>

   SetOutputFilter DEFLATE
   AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   BrowserMatch ^Mozilla/4\.0[678] no-gzip
   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
   BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
   SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
   Header append Vary User-Agent env=!dont-vary
</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>

and for Leverage browser caching your code is correct but if its not working then you have to check that mod_expires and mod_headers modules are enabled or not on your server. 并且对于Leverage浏览器缓存,您的代码是正确的,但如果它不起作用,那么您必须检查服务器上是否启用了mod_expires和mod_headers模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM