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

use*_*026 3 .htaccess browser-cache

想知道这可能在.htaccess吗?

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

然而有人建议它可以通过.htaccess而且速度更快,所以希望也许有人可以解决一些问题......我环顾四周,找到了各种片段,但没有一个能够覆盖我所追求的内容.

Cli*_*ive 6

如果你已经在你的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$>
    # 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>
Run Code Online (Sandbox Code Playgroud)