Navigation überspringen

Blog 6 Min. Lesezeit

Die perfekte .htaccess Datei für WordPress

von Mathias Arzberger

Die perfekte .htaccess Datei für WordPress

Die perfekte .htaccess Datei für WordPress

Die perfekte .htaccess Datei für WordPress für eine gute Platzierung in den Google Suchergebnissen und eine schnelle Webseite.

Der richtige Performanceschub für deine WordPress Webseite

Erhöhe die Performance und die Sicherheit deiner WordPress Seite mit einfachen Anpassungen in der Konfigurationsdatei .htaccess.

Kompimieren und cachen

Browser-Cache ist ein Puffer-Speicher des Webbrowsers, in dem bereits abgerufene Ressourcen auf dem Rechner des Benutzers als Kopie aufbewahrt werden. Wird eine Ressource später erneut benötigt, ist sie aus dem Cache schneller abrufbar. Ab dem 2. Aufruf einer Webseite hat der Benutzer bereits einen Geschwindigkeitsvorteil. Der Browser Cache bezieht sich hierbei nicht nur auf Bilder, sondern auch auf die HTML-Seiten selbst, sowie dessen Schriftarten, CSS- und Javascript-Dateien. Dies bedeutet für den Server natürlich auch weniger Last und spart Ressourcen.

Also los, alles cachen!

<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault                                      "access plus 1 month"
# CSS
ExpiresByType text/css                              "access plus 1 year"
# Data interchange
ExpiresByType application/atom+xml                  "access plus 1 hour"
ExpiresByType application/rdf+xml                   "access plus 1 hour"
ExpiresByType application/rss+xml                   "access plus 1 hour"
ExpiresByType application/json                      "access plus 0 seconds"
ExpiresByType application/ld+json                   "access plus 0 seconds"
ExpiresByType application/schema+json               "access plus 0 seconds"
ExpiresByType application/vnd.geo+json              "access plus 0 seconds"
ExpiresByType application/xml                       "access plus 0 seconds"
ExpiresByType text/xml                              "access plus 0 seconds"
# Favicon (cannot be renamed!) and cursor images
ExpiresByType image/vnd.microsoft.icon              "access plus 1 week"
ExpiresByType image/x-icon                          "access plus 1 week"
# HTML - Behält die Website eine Stunde im Cache, neues wird erst nach Ablauf einer Stunde
# angezeigt. Wenn nicht gewuenscht, bei 3600 eine Null eintragen
ExpiresByType text/html                             "access plus 3600 seconds"
# JavaScript
ExpiresByType application/javascript                "access plus 1 year"
ExpiresByType application/x-javascript              "access plus 1 year"
ExpiresByType text/javascript                       "access plus 1 year"
# Manifest files
ExpiresByType application/manifest+json             "access plus 1 week"
ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
ExpiresByType text/cache-manifest                   "access plus 0 seconds"
# Media files
ExpiresByType audio/ogg                             "access plus 1 month"
ExpiresByType image/bmp                             "access plus 1 month"
ExpiresByType image/gif                             "access plus 1 month"
ExpiresByType image/jpeg                            "access plus 1 month"
ExpiresByType image/png                             "access plus 1 month"
ExpiresByType image/svg+xml                         "access plus 1 month"
ExpiresByType image/webp                            "access plus 1 month"
ExpiresByType video/mp4                             "access plus 1 month"
ExpiresByType video/ogg                             "access plus 1 month"
ExpiresByType video/webm                            "access plus 1 month"
# Web fonts
# Embedded OpenType (EOT)
ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
ExpiresByType font/eot                              "access plus 1 month"
# OpenType
ExpiresByType font/opentype                         "access plus 1 month"
# TrueType
ExpiresByType application/x-font-ttf                "access plus 1 month"
# Web Open Font Format (WOFF) 1.0
ExpiresByType application/font-woff                 "access plus 1 month"
ExpiresByType application/x-font-woff               "access plus 1 month"
ExpiresByType font/woff                             "access plus 1 month"
# Web Open Font Format (WOFF) 2.0
ExpiresByType application/font-woff2                "access plus 1 month"
# Other
ExpiresByType text/x-cross-domain-policy            "access plus 1 week"
</IfModule>
<IfModule mod_deflate.c>
# Insert filters / compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/vtt
AddOutputFilterByType DEFLATE text/x-component
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/js
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/ld+json
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/font-woff2
AddOutputFilterByType DEFLATE application/x-font-woff
AddOutputFilterByType DEFLATE application/x-web-app-manifest+json font/woff
AddOutputFilterByType DEFLATE font/woff
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/webp
# Exception: Images
SetEnvIfNoCase REQUEST_URI \.(?:gif|jpg|jpeg|png|svg|webp)$ no-gzip dont-vary
# Drop problematic browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
#Alternative caching using Apache's "mod_headers", if it's installed.
#Caching of common files - ENABLED
<IfModule mod_headers.c>
<FilesMatch "\.(ico|pdf|flv|swf|js|css|gif|png|jpg|jpeg|txt|webp)$">
Header set Cache-Control "max-age=2628000, public"
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
# Set Keep Alive Header
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
# If your server don't support ETags deactivate with "None" (and remove header)
<IfModule mod_expires.c>
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
</IfModule>
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html|woff|woff2|ttf)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>

WordPress Sicherheit

Folgende Dateien solltest du schon von vornherein schützen. WordPress macht dies zwar auch, aber warum sollten wir erst PHP/WordPress bemühen um zu prüfen ob ein Zugriff erlaubt ist?

# Keinen Zugriff auf die wp-config.php Datei erlauben
<files wp-config.php>
Order allow,deny
Deny from all
</files>
# Keinen Zugriff auf die readme.html Datei erlauben
<files readme.html>
Order Allow,Deny
Deny from all
Satisfy all
</Files>
# Keinen Zugriff auf die liesmich.html Datei erlauben / deutsche Version
<Files liesmich.html>
Order Allow,Deny
Deny from all
Satisfy all
</Files>
# Keinen Zugriff auf die Dateien .htaccess und .htpasswd erlauben
<FilesMatch "(\.htaccess|\.htpasswd)">
Order deny,allow
Deny from all
</FilesMatch>
# Auslesen der Autoren über die ID verhindern
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*author=(.+.?) [NC]
RewriteRule (.*) /blog/?author= [NC,L,R=301]
</IfModule>

Weiterleitung von http auf https

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Bilderklau verhindern

In einigen Fällen kann es ratsam sein andere Webseiten oder Dienste daran zu hindern deine Bilddateien direkt ein zubinden (Hotlinking). Anstatt des echten Bildes kannst du einfach ein Platzhalterbild ausliefern.

[Mehr zum Thema Hotlinking verhindern]

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.de [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ https://platzhalter.png [NC,R,L]
</IfModule>

Fazit

Die .htaccess Datei ist ein wichtiges Werkzeug für Performance und Sicherheit deiner WordPress Webseite und sollte regelmäßig an aktuelle Bedürfnisse angepasst werden. Solltest du weitere Empfehlungen für eine Optimierung der .htaccess Datei haben, nehmen wir diese gern auf.

Interessante Themen

Die perfekte .htaccess Datei für WordPressContao 4 - Access-Control-Allow-OriginDie perfekte .htaccess Datei für ContaoContao Suchmaschinenoptimierung

Zurück zur Newsübersicht