# BYTE High-Performance Apache Configuration
RewriteEngine On
RewriteBase /

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Custom 404 Error Page with true HTTP 404 status
ErrorDocument 404 /404.html

# Trailing slash handling for existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

# Support clean directory URLs and direct HTML files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*)/?$ /$1.html [L]

# Explicit 301 Redirects for former/obsolete URL structures
RewriteRule ^glossar/?.*$ /lexikon/ [L,R=301]
RewriteRule ^blog/content-marketing-checklist/?$ /blog/das-richtige-autorenbriefing/ [L,R=301]
RewriteRule ^blog/warum-weg-von-wordpress/?$ /blog/websites-conversion-schnelle-web-systeme-ohne-wordpress-wien-niederoesterreich/ [L,R=301]
RewriteRule ^blog/local-seo-osterreich/?$ /blog/google-maps-optimierung-local-3-pack-wien-niederoesterreich/ [L,R=301]
RewriteRule ^blog/domainhandel-guide/?$ /blog/domain-backorder-drop-catching-guide-leo-kobes/ [L,R=301]

# If file or directory does not exist, and no corresponding .html file exists:
# Serve /404.html with real HTTP 404 status code instead of falling back to homepage index.html!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html !-f
RewriteRule ^.*$ /404.html [L,R=404]


# Gzip / Brotli Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Browser Caching Headers
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType text/html "access plus 0 seconds"
</IfModule>
