Wie lege ich das Ablaufdatum für CSS, JS und Bilder fest?

Lesezeit: 4 Minuten

Benutzer-Avatar
KoolKabin

Ich habe kürzlich meine Website mit dem Pagespeed-Addon auf Firebug analysiert. Es schlug mir vor, das Ablaufdatum für CSS-, JS- und Bilddateien festzulegen.

Ich frage mich, wie mache ich das?

  • Es hängt wirklich davon ab, welche Plattformen Sie verwenden: IIS6/7, Apache usw.

    – jordanbtucker

    10. November 2010 um 6:01 Uhr


  • Als Webserver verwende ich Apache. Ich zeige diese Dateien über meine PHP-Dateien oder in PHP-Dateien an

    – KoolKabin

    10. November 2010 um 6:04 Uhr

Dies ist diejenige, die ich verwende, um genau dasselbe Problem zu beheben, als ich das PageSpeed-Addon ausgeführt habe:

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Dies geht in Ihre .htaccess-Datei.

Lesen Sie auf dieser Seite nach, um weitere Informationen darüber zu erhalten, wie Sie den Cache für zusätzliche Dateitypen festlegen und/oder die Cache-Länge ändern können:

http://www.askapache.com/htaccess/apache-speed-cache-control.html

  • Ich habe den internen Serverfehler 500 erhalten. Ich denke, mein Apache header_module ist nicht aktiviert. habe ich einen anderen Weg?

    – KoolKabin

    10. November 2010 um 6:48 Uhr

  • Danke Kumpel, bei mir funktioniert es auch nach 3 Jahren

    – Atif Azad

    9. September 2014 um 7:45 Uhr


  • @Bhuwan Es ist die Anzahl der Sekunden (7 Tage)

    – Chuck Le Butt

    23. April 2018 um 12:20 Uhr

Benutzer-Avatar
Mohammed Sufian

Ich möchte diese Lösung für diejenigen hinzufügen, die danach suchen ….

es funktioniert auch großartig … mit .htaccess

https://webmasters.stackexchange.com/a/5275/37765

 <FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
  ExpiresActive On
  ExpiresDefault A2592000
 </FilesMatch>

  • Es hat für mich funktioniert. Ich möchte jedoch wissen, was passiert, wenn eine Website nach dem Hinzufügen dieses Codes unterbrochen wird.

    – Raul

    20. September 2017 um 10:09 Uhr

  • Ich musste zuerst das Modul einbinden: ln -s /etc/apache2/mods-available/expires.load /etc/apache2/mods-enabled/ (gefunden hier), obwohl das Modul bereits mit aktiviert wurde a2enmod headers Ich habe 500

    – Wladkras

    16. Juni 2019 um 11:07 Uhr


Ich erstelle eine Datei “expires.conf” und füge sie in die Site-Dateikonfiguration von Apache ein. Sie können in .htaccess einschließen, wenn Sie möchten. Meine läuft ab:

<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 text/html "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 1 month"
</IfModule>

Sie müssen das Expires-Modul in Apache aktivieren.

Add web.config file in your solution to remove "expiration not specified"

<configuration>
<system.webServer>
<staticContent>
  <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</configuration>

Benutzer-Avatar
T.Todua

Besser (gefunden bei http://www.paulund.co.uk/set-expire-headers-in-htaccessaber 0 seconds hat nicht funktioniert, ich habe es geändert 1 seconds )

# These are pretty far-future expires headers
# They assume you control versioning with cachebusting query params like:  <script src="https://stackoverflow.com/questions/4141643/application.js?20100608">
# Additionally, consider that outdated proxies may miscache
#
#   www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
# If you don`t use filenames to version, lower the css and js to something like "access plus 1 week"

<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 1 seconds"


# Your document html
  ExpiresByType text/html                 "access plus 1 seconds"

# Data
  ExpiresByType text/xml                  "access plus 1 seconds"
  ExpiresByType application/xml           "access plus 1 seconds"
  ExpiresByType application/json          "access plus 1 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 1 seconds"
  ExpiresByType application/javascript    "access plus 1 month"
  ExpiresByType text/javascript           "access plus 1 month"

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

</IfModule>

1205080cookie-checkWie lege ich das Ablaufdatum für CSS, JS und Bilder fest?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy