.htaccess 缓存静态内容(除非修改)?

发布于 2024-12-06 06:14:44 字数 189 浏览 0 评论 0原文

想知道这在 .htaccess 中是否可能?

我目前正在通过 PHP 缓存 .js、.css 和所有图像文件(并且仅当文件未通过检查 filemtime() 修改时才提供缓存)。

然而,有人建议可以通过 .htaccess 并且更快,所以希望有人可以提供一些线索...我环顾四周,发现了各种片段,但没有一个片段涵盖了我所追求的内容。

Was wondering is this possible in .htaccess?

I'm currently caching .js, .css and all image files via PHP (and providing the cached only if the file has not been modified by checking the filemtime()).

However someone suggested it's possible via .htaccess and much faster, so was hoping maybe someone can shed some light...I've looked around and found various snippets but none which cover what I'm after.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亚希 2024-12-13 06:14:44

如果你的 apache 服务器上安装了 mod_expires 你可以放一些东西就像在您的 .htaccess 文件中一样。这个示例是面向 PHP 的(实际上是从 Drupal 7 .htaccess 文件中获取的),但应该作为一个很好的起点。

FileETag MTime Size
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php
gt;
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior.
    ExpiresActive Off
  </FilesMatch>
</IfModule>

If you've got mod_expires installed on your apache server you can put something like this in your .htaccess file. This example is PHP orientated (actually grabbed from the Drupal 7 .htaccess file) but should serve as a good starting point.

FileETag MTime Size
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php
gt;
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves. Otherwise all scripts would have to overwrite the
    # headers set by mod_expires if they want another caching behavior.
    ExpiresActive Off
  </FilesMatch>
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文