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

标签 php .htaccess optimization magento-1.9

我正在尝试优化我的商店,我在我的 .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 #

最佳答案

启用 Apache mod_headers 和 mod_expires 模块

Please check mod_expires and mod_headers are enable or not on your store through the below code.

<?php phpinfo();?>

If both extension not unable to your server,please follow following steps:-

第 1 步:-

Now login to server using SSH console and continue below steps:

启用 mod_headers:

sudo a2enmod headers

启用 mod_expires:

sudo a2enmod expires

Step 2 :-

after completing the server update, you need to restart Apache server to make these changes effective. Enter below line in your SSH console to restart Apache.

service apache2 restart

Or else reboot your server by following code in SSH:

reboot

在您的 .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>

清除商店缓存并重新加载网站 URL 后,重新检查 gtmetrix 工具或您正在使用的任何工具。

关于php - Gzip 压缩和利用浏览器缓存不适用于我的商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369190/

相关文章:

PHP 连接查询返回不需要的结果

mysql - PHP 错误 : Maximum function nesting level of '100' reached, 中止

php - 通过 CURL 登录 htpasswd 并将 session 保留在浏览器中

php - 如何通过 .htaccess 创建动态目录和子目录

c - C 条件语句中的 "And"和 "Or"运算符

c++ - 关于循环变量优化的标准合规行为是什么?

php - 打开模态时保留变量的值

php - 从 MySQL 中的分层数据中获取数据

apache - mod_rewrite 重映射文件夹

javascript - 分析性能敏感的代码路径