2017-07-28 64 views
4

我试图优化我的店,我已经加入Gzip已代码和杠杆浏览器缓存代码在我.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 # 
+0

您是否检查过在Apache中启用了这些模块?你如何检查它是否被使用? –

+0

您问过的好问题,不断问及在这里分享知识@ Randhir –

+0

谢谢@DeepakKumar – Randhir

回答

5

启用mod_headers中的Apache和指定mod_expires模块

请检查指定mod_expires和mod_headers中通过下面的代码在你的商店启用与否。

<?php phpinfo();?> 

如果两个扩展不无法将您的服务器,请按照下列步骤操作: -

第1步: -

现在使用SSH登录到服务器控制台并继续以下步骤:

要启用mod_headers中:

sudo a2enmod headers 

要启用指定mod_expires:

sudo a2enmod expires 

第2步: -

完成服务器更新后,您需要重启Apache服务器 以使这些更改生效。在您的SSH控制台 中输入以下行重新启动Apache。

service apache2 restart 

或者通过在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工具或任何您正在使用的工具。

+0

谢谢你的详细解释,你让我的一天..现在工作正常,我检查了GTmetrix工具我的网站速度看起来不错。再次感谢@ Umesh Kumar – Randhir

+0

很好的解释,谢谢 –

+0

真的很好回答。 –

0

这里是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> 

并充分利用浏览器缓存你的代码是正确的,但如果没有,那么你的工作有检查指定mod_expires和mod_headers中模块在服务器上启用。