15

ウェブサイトでSpeedTestをテストすると、有効期限が指定されていないというエラーが多数表示されます。このページで確認できます。

このコードを.htaccessファイルに追加しました

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##

しかし、何も変わりませんでした。静的ファイルの有効期限を指定できますか?

サーバー:Linux-Apache

4

5 に答える 5

39

私はhtml5boilerplateの方法が好きです:

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"

# your document html 
  ExpiresByType text/html                 "access plus 0 seconds"

# data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"

# rss feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week" 

# media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"

# htc files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"

# webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# css and javascript
  ExpiresByType text/css                  "access plus 2 months"
  ExpiresByType application/javascript    "access plus 2 months"
  ExpiresByType text/javascript           "access plus 2 months"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

これがお役に立てば幸いです。ソース: https ://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

于 2011-04-13T19:20:02.240 に答える
5

mod_headers を使用してトリックを実行できます。

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
  Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

または、mod_expires を使用できます。

ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"
于 2011-04-13T18:02:06.297 に答える
1

PHPがインストールされているようです(ワードプレスを参照)。問題の診断を試みます。

  1. ApacheWebサーバーを再起動します。Apacheサーバー(コマンドラインアクセス)を所有している場合は、そのように行うことができます。
  2. phpinfoページを作成します。これにより、インストールされているapacheモジュールを表示できます。使用しようとしているmod_expires.cモジュールを確認します(ブラウザでmod_expiresのCTL-F(検索)要求を実行できる必要があります)。あなたがそれを見つけたら-GOOD!そうでない場合は、Apacheサーバーにmod_expiresをインストールする必要があります。Apacheサーバーを所有している場合は、特定のオペレーティングシステム用のモジュールをインストールする方法を検索できます。そうでない場合は、このモジュールをインストールする方法についてホスティングプロバイダーに確認できます。インストールしたら、次に進みます。
  3. http.confエントリを変更して、式に「plus」を含めます
  4. Apacheを再起動します
  5. テスト=)
于 2011-04-13T18:11:19.467 に答える
1

以下を追加するだけです:

ExpiresByType application/javascript "access plus 1 year"
于 2015-10-08T13:36:45.273 に答える
0

mod_expires をロードしていない可能性があるため、スタンザ全体がノーオペレーションです。または、AllowOverride が None で、.htaccess ファイルが処理されていません。

于 2011-04-07T10:43:14.367 に答える