【问题标题】:Set expiration browser date for external file in htaccess在 htaccess 中设置外部文件的到期浏览器日期
【发布时间】:2018-09-05 13:53:46
【问题描述】:

我想针对 Google 优化我的网站。 我已经加载了用于分析的外部 js 文件。 我在 .htaccess 中创建了 mod_expires 的其他文件,但在这种情况下我不知道如何处理。

Google 告诉我:

利用浏览器缓存

如果您在 HTTP 标头中设置了过期日期或最长期限 静态资源,浏览器加载之前下载的资源 从本地磁盘而不是从网络。

对以下可缓存资源使用浏览器缓存:

http://www.google-analytics.com/analytics.js(2 小时)

我真的是学徒,谁会帮助我。

用我现在的东西编辑

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS
    ExpiresByType text/css                              "access plus 1 year"

  # Data interchange
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rdf+xml                   "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/ld+json                   "access plus 0 seconds"
    ExpiresByType application/schema+json               "access plus 0 seconds"
    ExpiresByType application/vnd.geo+json              "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"

  # Favicon (cannot be renamed!) and cursor images
    ExpiresByType image/vnd.microsoft.icon              "access plus 1 week"
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML
    ExpiresByType text/html                             "access plus 0 seconds"

  # JavaScript
    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-javascript              "access plus 1 year"
    ExpiresByType text/javascript                       "access plus 1 year"

  # Manifest files
    ExpiresByType application/manifest+json             "access plus 1 year"

    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"

  # Media files
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/bmp                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
    ExpiresByType image/webp                            "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"

  # Web fonts

    # Embedded OpenType (EOT)
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType font/eot                              "access plus 1 month"

    # OpenType
    ExpiresByType font/opentype                         "access plus 1 month"

    # TrueType
    ExpiresByType application/x-font-ttf                "access plus 1 month"

    # Web Open Font Format (WOFF) 1.0
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/x-font-woff               "access plus 1 month"
    ExpiresByType font/woff                             "access plus 1 month"

    # Web Open Font Format (WOFF) 2.0
    ExpiresByType application/font-woff2                "access plus 1 month"

  # Other
    ExpiresByType text/x-cross-domain-policy            "access plus 1 week"

</IfModule>

【问题讨论】:

  • 在没有插件的情况下在htaccess中设置要困难得多,你会考虑通过php发送header吗?
  • 没关系,但是我应该在 php 中加载外部文件怎么办?
  • 所以你想缓存analytics.js到客户端的浏览器?
  • 我想为谷歌服务器上的静态资源analytics.js设置一个过期日期
  • 我认为你将无法在谷歌服务器上设置缓存过期。

标签: javascript html .htaccess caching cache-control


【解决方案1】:

.htaccess

# 1 Month for all your static assets
 <filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
 Header set Cache-Control "max-age=2592000, public"
 </filesMatch>

 # 1 DAYS for rss feeds and robots
 <filesMatch ".(xml|txt)$">
 Header set Cache-Control "max-age=86400, public, must-revalidate"
 </filesMatch>

 # 4 HOURS for your real articles files
 <filesMatch ".(html|htm)$">
 Header set Cache-Control "max-age=14400, must-revalidate"
 </filesMatch>

【讨论】:

  • 我用我现在拥有的东西进行编辑,不幸的是你写给我的东西我仍然有问题。我只是将您的代码添加到我在问题中复制粘贴的内容之上(已编辑)。
【解决方案2】:

我找到了答案:

PageSpeed Insights 99/100 because of Google Analytics - How can I cache GA?

解决办法如下:

<?php if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos($_SERVER['HTTP_USER_AGENT'], 'Speed Insights') === false): ?>
// your analytics code here
<?php endif; ?>

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2021-01-05
    相关资源
    最近更新 更多