Effiziente Verweildauer im Cache verwenden

Eine effiziente Verweildauer (TTL – Time to Live) im Cache optimiert die Ladezeiten, indem statische Ressourcen (Bilder, CSS, JS) für mindestens 30 Tage, idealerweise bis zu 1 Jahr, zwischengespeichert werden. Dies senkt die Serverlast und verbessert die Google-Rankingfaktoren. Die Strategie umfasst lange Cache-Zeiten für statische Dateien und kurze oder keine für dynamische Inhalte, um Aktualisierungen zu gewährleisten.

Pagespeed Insight use efficient cache lifetimes

Client Caching Code für die .htaccess

htaccess in Yoast SEO

Die .htaccess-Datei lässt sich in Yoast SEO direkt über 
SEO > Werkzeuge > Datei-Editor bearbeiten.
Dort können Regeln wie Weiterleitungen oder Sicherheitseinstellungen vorgenommen werden. Wichtig: Erstellen Sie unbedingt vor Änderungen ein Backup der Datei, da fehlerhafte Einträge zu einem „Error 500“ führen können. 

# =============================================================================
# BROWSER CACHING — Google PageSpeed "Use efficient cache lifetimes"
# Compatible with Apache 2.2 / 2.4  |  Requires mod_expires + mod_headers
# Place this block in your root .htaccess (WordPress sites: after the
# WordPress core rewrite rules, or merge carefully if rules already exist).
# =============================================================================

# ── 1. Enable mod_expires ────────────────────────────────────────────────────
<IfModule mod_expires.c>

    ExpiresActive On

    # Default fallback (rarely reached — explicit rules below cover everything)
    ExpiresDefault                              "access plus 1 month"

    # ── HTML / Dynamic Pages ─────────────────────────────────────────────────
    # WordPress pages are dynamic; keep short so changes propagate quickly.
    ExpiresByType text/html                     "access plus 0 seconds"

    # ── Feeds ────────────────────────────────────────────────────────────────
    ExpiresByType application/rss+xml           "access plus 1 hour"
    ExpiresByType application/atom+xml          "access plus 1 hour"

    # ── CSS & JavaScript ─────────────────────────────────────────────────────
    # WordPress appends ?ver=x.x.x query strings, so 1 year is safe.
    ExpiresByType text/css                      "access plus 1 year"
    ExpiresByType text/javascript               "access plus 1 year"
    ExpiresByType application/javascript        "access plus 1 year"
    ExpiresByType application/x-javascript      "access plus 1 year"

    # ── Images ───────────────────────────────────────────────────────────────
    ExpiresByType image/gif                     "access plus 1 year"
    ExpiresByType image/jpeg                    "access plus 1 year"
    ExpiresByType image/jpg                     "access plus 1 year"
    ExpiresByType image/png                     "access plus 1 year"
    ExpiresByType image/webp                    "access plus 1 year"
    ExpiresByType image/avif                    "access plus 1 year"
    ExpiresByType image/svg+xml                 "access plus 1 year"
    ExpiresByType image/x-icon                  "access plus 1 year"
    ExpiresByType image/vnd.microsoft.icon      "access plus 1 year"

    # ── Fonts ────────────────────────────────────────────────────────────────
    ExpiresByType font/ttf                      "access plus 1 year"
    ExpiresByType font/otf                      "access plus 1 year"
    ExpiresByType font/woff                     "access plus 1 year"
    ExpiresByType font/woff2                    "access plus 1 year"
    ExpiresByType application/font-woff         "access plus 1 year"
    ExpiresByType application/font-woff2        "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"

    # ── Video / Audio ────────────────────────────────────────────────────────
    ExpiresByType video/mp4                     "access plus 1 year"
    ExpiresByType video/webm                    "access plus 1 year"
    ExpiresByType audio/mpeg                    "access plus 1 year"
    ExpiresByType audio/ogg                     "access plus 1 year"

    # ── Documents ────────────────────────────────────────────────────────────
    ExpiresByType application/pdf               "access plus 1 month"

    # ── Data / Manifest ──────────────────────────────────────────────────────
    ExpiresByType application/json              "access plus 0 seconds"
    ExpiresByType application/xml               "access plus 0 seconds"
    ExpiresByType text/xml                      "access plus 0 seconds"
    ExpiresByType application/manifest+json     "access plus 1 week"
    ExpiresByType text/cache-manifest           "access plus 0 seconds"

</IfModule>


# ── 2. Set Cache-Control headers (mod_headers) ───────────────────────────────
# Adds explicit Cache-Control max-age to reinforce the Expires header and
# satisfies PageSpeed's recommendation even when mod_expires alone isn't enough.
<IfModule mod_headers.c>

    # Versioned static assets — cache aggressively
    <FilesMatch "\.(css|js|gif|jpe?g|png|webp|avif|svg|ico|woff2?|ttf|otf|eot|mp4|webm)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # Fonts — always public so CDN / proxy can cache them too
    <FilesMatch "\.(woff2?|ttf|otf|eot)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
        # Required for cross-origin font requests (e.g. Google Fonts self-hosting)
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

    # PDFs — cache for a month
    <FilesMatch "\.pdf$">
        Header set Cache-Control "public, max-age=2592000"
    </FilesMatch>

    # HTML — must revalidate every time
    <FilesMatch "\.html?$">
        Header set Cache-Control "no-cache, must-revalidate"
    </FilesMatch>

    # Remove ETags — prevents redundant revalidation round-trips
    # (only beneficial if you also set Expires / Cache-Control above)
    Header unset ETag

</IfModule>


# ── 3. Disable ETags (file-system level) ─────────────────────────────────────
# Ensures stale ETags don't override Cache-Control on servers that still
# generate them even after the Header unset above.
FileETag None


# =============================================================================
# QUICK-REFERENCE — Cache lifetimes used above
# -----------------------------------------------------------------------------
#  0 seconds  → HTML, JSON, XML, feeds  (always fresh / dynamic)
#  1 hour     → RSS / Atom feeds
#  1 week     → Web App Manifest
#  1 month    → PDFs
#  1 year     → CSS, JS, images, fonts, video  (WordPress adds ?ver= busters)
# =============================================================================

.htaccess in RankMath editieren

Die .htaccess-Datei lässt sich in Rank Math direkt über das WordPress-Dashboard unter Rank Math SEO > Allgemeine Einstellungen > .htaccess bearbeiten anpassen. Der Modus muss hierfür auf „Erweitert“ eingestellt sein. Es wird dringend empfohlen, vor Änderungen ein Backup zu erstellen, da fehlerhafte Einträge die Website unerreichbar machen können.

Empfohlene Cache-Laufzeiten

Die optimale Dauer hängt vom Ressourcentyp ab:

  • Statische Assets (Bilder, Schriften, Medien): Diese sollten laut Google Chrome Developer Docs eine Cache-Lebensdauer von mindestens 30 Tagen (bis zu einem Jahr) haben.
  • Drittanbieter-Ressourcen (Widgets, Ads): Hier wird eine Dauer von mindestens einem Tag empfohlen.
  • Regelmäßige Inhalte: Für Ressourcen, die sich gelegentlich ändern, wird oft eine Mindestdauer von einer Woche angesetzt.

Best Practices zur Implementierung

Um die Verweildauer effizient zu steuern, sollten Sie folgende Techniken nutzen:

  • HTTP-Header: Verwenden Sie Cache-Control (z. B. max-age=31536000) oder Expires, um dem Browser mitzuteilen, wie lange er eine Kopie speichern darf.
  • ETags: Nutzen Sie ETags als Validierungstoken. Der Browser prüft damit beim Server, ob sich eine Datei geändert hat, bevor er sie erneut herunterlädt.
  • Cache-Busting: Um Probleme mit veralteten Inhalten bei langen Laufzeiten zu vermeiden (z. B. nach einem Update), sollten Sie Dateinamen mit Fingerprints oder Versionierungen versehen (z. B. style.v2.css).

Weiterführende Quellen und Tools

  • Google PageSpeed Insights: Bietet detaillierte Anleitungen, wie Sie die Browser-Caching-Richtlinien für Ihre spezifische Seite optimieren.
  • web.dev – Long-term Caching: Erklärt fortgeschrittene Strategien wie Webpack-Integration für langfristiges Caching.
  • HostEurope Blog: Bietet praxisnahe Tipps zur Einstellung von Expires-Headern für verschiedene CMS-Systeme. Google for Developers +2

Möchten Sie wissen, wie Sie diese Header konkret in Ihrer .htaccess oder Nginx-Konfiguration hinterlegen?

Weiterführende Quellen

Eine effiziente Verweildauer im Cache reduziert unnötige Serveranfragen und verbessert die Ladezeit Ihrer Webseite erheblich. Die folgenden Empfehlungen und Quellen helfen Ihnen bei der optimalen Konfiguration: