# ==========================================================================
# Alba-Zochling s.r.o. - Apache Configuration
# ==========================================================================

# ---- Custom error page ----
ErrorDocument 404 /404.html

# ---- Hidden files and folders (.git, .htaccess, ...) are never served ----
RewriteEngine On
RewriteRule (^|/)\.(?!well-known/) - [F,L]

# ---- Force HTTPS ----
# Synology Web Station terminates TLS in its nginx front end and hands Apache plain HTTP,
# so %{HTTPS} alone would loop forever; trust X-Forwarded-Proto from the front end too.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ---- Force www (each domain to its own www variant) ----
RewriteCond %{HTTP_HOST} ^alba-zochling\.(cz|hu)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ---- Domain root → branch default language (ccTLD split) ----
RewriteCond %{HTTP_HOST} alba-zochling\.hu$ [NC]
RewriteRule ^$ https://%{HTTP_HOST}/hu/ [L,R=302]
RewriteRule ^$ https://%{HTTP_HOST}/cs/ [L,R=302]

# ---- ccTLD mount: domain selects the root folder (cz/ vs hu/) ----
# .hu serves from hu/, .cz from cz/ → the country is not in the URL (/en/ on .cz = cz/en/, on .hu = hu/en/).
# Each domain STRICTLY serves only its own branch → a cross-domain path (/cs/ on .hu) = 404, no leak.
# Two rules per domain (-d and -f separately); host MUST match = clean AND (avoids tricky [OR] precedence).
# The existence check lets shared /assets/ and /components/ fall through to root and prevents loops.
# Adding another country = one more pair of rules for the new domain.
RewriteCond %{HTTP_HOST} alba-zochling\.hu$ [NC]
RewriteCond %{DOCUMENT_ROOT}/hu/$1 -d
RewriteRule ^(.*)$ /hu/$1 [L]
RewriteCond %{HTTP_HOST} alba-zochling\.hu$ [NC]
RewriteCond %{DOCUMENT_ROOT}/hu/$1 -f
RewriteRule ^(.*)$ /hu/$1 [L]
RewriteCond %{HTTP_HOST} alba-zochling\.cz$ [NC]
RewriteCond %{DOCUMENT_ROOT}/cz/$1 -d
RewriteRule ^(.*)$ /cz/$1 [L]
RewriteCond %{HTTP_HOST} alba-zochling\.cz$ [NC]
RewriteCond %{DOCUMENT_ROOT}/cz/$1 -f
RewriteRule ^(.*)$ /cz/$1 [L]

# ---- Directory index ----
DirectoryIndex index.php index.html

# ---- GZIP compression ----
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE text/plain
</IfModule>

# ---- Browser caching ----
<IfModule mod_expires.c>
  ExpiresActive On

  # Images - 6 months
  ExpiresByType image/webp "access plus 6 months"
  ExpiresByType image/png "access plus 6 months"
  ExpiresByType image/x-icon "access plus 6 months"
  ExpiresByType image/svg+xml "access plus 6 months"

  # Fonts - 1 year
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType application/font-woff2 "access plus 1 year"

  # CSS and JS - 1 month (cache-busted via query string)
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # HTML - no cache (always fresh)
  ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# ---- Security headers ----
<IfModule mod_headers.c>
  # Prevent MIME-type sniffing
  Header set X-Content-Type-Options "nosniff"

  # Clickjacking protection
  Header set X-Frame-Options "SAMEORIGIN"

  # XSS filter
  Header set X-XSS-Protection "1; mode=block"

  # Referrer policy
  Header set Referrer-Policy "strict-origin-when-cross-origin"

  # Permissions policy
  Header set Permissions-Policy "camera=(), microphone=(), geolocation=()"

  # HSTS - force HTTPS for 1 year (including subdomains)
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

  # Content Security Policy
  Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'"

  # Cache control for fonts (immutable)
  <FilesMatch "\.(woff2)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # Cache control for images
  <FilesMatch "\.(webp|png|ico|svg)$">
    Header set Cache-Control "public, max-age=15768000"
  </FilesMatch>
</IfModule>

# ---- Prevent directory listing ----
Options -Indexes

# ---- Block access to sensitive files ----
<FilesMatch "(^\.|\.(md|json)$)">
  <IfModule mod_authz_core.c>
    Require all denied
  </IfModule>
</FilesMatch>

# Allow specific public files
<FilesMatch "^(robots\.txt|sitemap\.xml|llms\.txt|llms-full\.txt)$">
  <IfModule mod_authz_core.c>
    Require all granted
  </IfModule>
</FilesMatch>

# ---- UTF-8 encoding ----
AddDefaultCharset UTF-8

# ---- Correct MIME types ----
<IfModule mod_mime.c>
  AddType text/html .html
  AddType text/css .css
  AddType application/javascript .js
  AddType application/json .json
  AddType image/webp .webp
  AddType image/svg+xml .svg
  AddType font/woff2 .woff2
  AddType text/plain .txt
  AddType text/xml .xml
</IfModule>
