# New Braunfels Tile Installers: Apache config (mod_rewrite, mod_headers, mod_expires, mod_deflate/mod_brotli)

DirectoryIndex index.html
Options -Indexes -MultiViews
# /projects is both a page (projects.html) and a folder (projects/); never auto-append a slash
DirectorySlash Off
AddDefaultCharset UTF-8

# ---------------------------------------------------------------- private files
# Lead logs, CSV exports and dotfiles are never served.
<IfModule mod_authz_core.c>
  <FilesMatch "(\.(csv|log)$|^\.)">
    Require all denied
  </FilesMatch>
</IfModule>
<IfModule !mod_authz_core.c>
  <FilesMatch "(\.(csv|log)$|^\.)">
    Order allow,deny
    Deny from all
  </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # Deny dot-directories (e.g. /.git/) as well as dotfiles
  RewriteRule (^|/)\.(?!well-known/) - [F,L]

  # Collapse duplicate slashes
  RewriteCond %{THE_REQUEST} \s[^?]*//
  RewriteRule ^(.*)$ https://newbraunfelstileinstallers.com/$1 [R=301,L,NE]

  # Force HTTPS (proxy-aware) and strip www, in one redirect
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !=https
  RewriteRule ^ https://newbraunfelstileinstallers.com%{REQUEST_URI} [R=301,L,NE]
  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteRule ^ https://newbraunfelstileinstallers.com%{REQUEST_URI} [R=301,L,NE]

  # No index pages for /services and /service-areas
  RewriteRule ^(services|service-areas)/?$ https://newbraunfelstileinstallers.com/ [R=301,L]

  # */index.html -> */
  RewriteCond %{THE_REQUEST} \s/+(.*/)?index\.html[\s?] [NC]
  RewriteRule ^(.*/)?index\.html$ https://newbraunfelstileinstallers.com/%1 [R=301,L,NE]

  # /page.html -> /page
  RewriteCond %{THE_REQUEST} \s/+([^?\s]+)\.html[\s?] [NC]
  RewriteRule ^ https://newbraunfelstileinstallers.com/%1 [R=301,L,NE]

  # Strip the trailing slash from anything that is not a real directory, or that is also a page (/projects/)
  RewriteCond %{REQUEST_FILENAME} !-d [OR]
  RewriteCond %{DOCUMENT_ROOT}/$1.html -f
  RewriteRule ^(.+)/$ https://newbraunfelstileinstallers.com/$1 [R=301,L]

  # /page -> page.html (internal)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}/$1.html -f
  RewriteRule ^(.+)$ $1.html [L]
</IfModule>

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

# ---------------------------------------------------------------- types + caching
<IfModule mod_mime.c>
  AddType image/webp .webp
  AddType font/woff2 .woff2
  AddType application/manifest+json .webmanifest
</IfModule>
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 hour"
  ExpiresByType text/html "access plus 1 hour"
  ExpiresByType text/css "access plus 30 days"
  ExpiresByType application/javascript "access plus 30 days"
  ExpiresByType text/javascript "access plus 30 days"
  ExpiresByType application/json "access plus 30 days"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
  <FilesMatch "\.(webp|jpe?g|png|svg|ico|woff2)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "\.(css|js|json|webmanifest)$">
    Header set Cache-Control "public, max-age=2592000"
  </FilesMatch>
  <FilesMatch "\.html$">
    Header set Cache-Control "public, max-age=3600"
  </FilesMatch>
  <FilesMatch "\.php$">
    Header set Cache-Control "no-store"
  </FilesMatch>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
