利用网站

时间:2016-05-08 08:05:30

标签: apache .htaccess caching browser-cache mod-expires

我使用magento和anguar js开发了我的网站。从谷歌网页见解,我得到了我必须利用浏览器缓存。所以为此,我已经定义了我的.htaccess文件:

 RewriteEngine On 
Options FollowSymLinks

RewriteBase /

RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule ^(.*)$ story.php?id=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]

<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>

但这不起作用,因为谷歌洞察仍然显示相同的信息。

1 个答案:

答案 0 :(得分:0)

您应该使用mod_expires检查在一定时间后应重新加载哪些资产。在.htaccess文件中,您可以这样配置:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 2 days"
    ExpiresByType image/gif "access plus 2592000 seconds"
    ExpiresByType image/jpeg "access plus 2592000 seconds"
    ExpiresByType image/png "access plus 2592000 seconds"
    ExpiresByType text/javascript "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/javascript "access 1 month"
    ExpiresByType application/x-javascript "access 1 month"
    ExpiresByType application/json "access 1 month"
</IfModule>

如您所见,访问时间为ExpiresDefault,为期2年。对于所有图像和javascripts,您可以发现时间。如果您需要其他值,可以在http://httpd.apache.org/docs/current/mod/mod_expires.html

找到大量文档