# ================================================
# Kala Web Studio - Apache .htaccess Configuration
# Optimized for Performance, Security, and SEO
# ================================================

# Enable Rewrite Engine
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Force HTTPS (SSL) - CRITICAL FOR SEO
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Force WWW or non-WWW (Choose one - currently set to non-WWW)
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    # Remove trailing slashes from URLs (except directories)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [R=301,L]
    
    # Clean URLs - Remove .html extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html [L]
</IfModule>

# ================================================
# GZIP Compression - Improve Page Load Speed
# ================================================
<IfModule mod_deflate.c>
    # Compress HTML, CSS, JavaScript, Text, XML and fonts
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-otf
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
    
    # Remove browser bugs (only needed for really old browsers)
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    Header append Vary User-Agent
</IfModule>

# ================================================
# Browser Caching - Leverage Browser Cache
# ================================================
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images
    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 image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    
    # Video
    ExpiresByType video/mp4 "access plus 1 year"
    ExpiresByType video/webm "access plus 1 year"
    
    # Audio
    ExpiresByType audio/mp3 "access plus 1 year"
    ExpiresByType audio/mpeg "access plus 1 year"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    
    # 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"
    
    # HTML
    ExpiresByType text/html "access plus 1 hour"
    
    # Default
    ExpiresDefault "access plus 1 month"
</IfModule>

# ================================================
# Cache-Control Headers
# ================================================
<IfModule mod_headers.c>
    # Images
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|svg|ico)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
    
    # CSS, JavaScript
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "max-age=2628000, public"
    </FilesMatch>
    
    # Fonts
    <FilesMatch "\.(ttf|otf|woff|woff2|eot)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
    
    # HTML - No caching
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=3600, public, must-revalidate"
    </FilesMatch>
</IfModule>

# ================================================
# Security Headers - Protect Against Attacks
# ================================================
<IfModule mod_headers.c>
    # X-Frame-Options - Prevent Clickjacking
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # X-Content-Type-Options - Prevent MIME sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # X-XSS-Protection - Enable XSS Protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Referrer-Policy - Control referrer information
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Permissions-Policy - Control browser features
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    
    # Content-Security-Policy - Prevent XSS and data injection attacks
    # Note: Adjust this based on your actual needs
    Header always set Content-Security-Policy "default-src 'self' https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://www.google-analytics.com https://www.clarity.ms https://fonts.googleapis.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: https:; connect-src 'self' https://www.google-analytics.com https://www.googletagmanager.com https://formsubmit.co;"
    
    # Strict-Transport-Security - Force HTTPS
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</IfModule>

# ================================================
# Protect Sensitive Files
# ================================================
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

<FilesMatch "(^#.*#|\.(bak|conf|dist|fla|in[ci]|log|orig|psd|sh|sql|sw[op])|~)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ================================================
# Prevent Directory Browsing
# ================================================
Options -Indexes

# ================================================
# Error Pages - SEO Friendly
# ================================================
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
ErrorDocument 500 /404.html

# ================================================
# MIME Types - Ensure proper file handling
# ================================================
<IfModule mod_mime.c>
    # JavaScript
    AddType application/javascript js
    AddType application/x-javascript js
    
    # JSON
    AddType application/json json
    
    # Web fonts
    AddType font/opentype otf
    AddType font/ttf ttf
    AddType application/font-woff woff
    AddType font/woff2 woff2
    
    # Images
    AddType image/svg+xml svg svgz
    AddType image/webp webp
    
    # Video
    AddType video/mp4 mp4
    AddType video/webm webm
</IfModule>

# ================================================
# UTF-8 Encoding
# ================================================
AddDefaultCharset UTF-8
<IfModule mod_mime.c>
    AddCharset UTF-8 .html .css .js .xml .json .rss .atom
</IfModule>

# ================================================
# ETags - Improve caching
# ================================================
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
FileETag None

# ================================================
# Cross-Origin Resource Sharing (CORS)
# ================================================
<IfModule mod_headers.c>
    # Allow fonts from any origin
    <FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

# ================================================
# PHP Settings (if applicable)
# ================================================
<IfModule mod_php7.c>
    php_value upload_max_filesize 64M
    php_value post_max_size 64M
    php_value max_execution_time 300
    php_value max_input_time 1000
</IfModule>
