编程语言
首页 > 编程语言> > php – Gzip Compression和杠杆浏览器缓存不适用于我的商店

php – Gzip Compression和杠杆浏览器缓存不适用于我的商店

作者:互联网

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

清除商店缓存并重新加载网站网址后,请重新检查gtmetrix工具或使用工具的任何内容.

标签:php,optimization,htaccess,magento-1-9
来源: https://codeday.me/bug/20190608/1196442.html