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