# =============================================================================
# LARAVEL APACHE .HTACCESS
# =============================================================================

# -----------------------------------------------------------------------------
# Enable rewrite engine
# -----------------------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} ^(.*)$
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    
    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# -----------------------------------------------------------------------------
# GZIP COMPRESSION (mod_deflate)
# -----------------------------------------------------------------------------
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    
    # Compress by MIME type
    AddOutputFilterByType DEFLATE application/atom+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/ld+json
    AddOutputFilterByType DEFLATE application/manifest+json
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-web-app-manifest+json
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE font/woff
    AddOutputFilterByType DEFLATE font/woff2
    AddOutputFilterByType DEFLATE image/bmp
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/cache-manifest
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/vcard
    AddOutputFilterByType DEFLATE text/vnd.rim.location.xloc
    AddOutputFilterByType DEFLATE text/vtt
    AddOutputFilterByType DEFLATE text/x-component
    AddOutputFilterByType DEFLATE text/x-cross-domain-policy
    
    # Level compression (Note: DeflateCompressionLevel cannot be set in .htaccess)
    # It must be configured in Apache's virtual host or server config
    # DeflateCompressionLevel 6
    
    # Skip already compressed files
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|webp|avif|zip|gz|bz2|xz|rar|7z|mp3|mp4|webm|woff2?)$ no-gzip dont-vary
</IfModule>

# -----------------------------------------------------------------------------
# BROWSER CACHING (mod_expires)
# -----------------------------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images - 1 year
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/avif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    
    # Fonts - 1 year
    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"
    ExpiresByType application/font-woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    
    # CSS/JS - 1 year
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    
    # Default
    ExpiresDefault "access plus 1 month"
</IfModule>

# -----------------------------------------------------------------------------
# CACHE CONTROL HEADERS (mod_headers)
# -----------------------------------------------------------------------------
<IfModule mod_headers.c>
    # Static assets with long cache
    <FilesMatch "\.(css|js|woff|woff2|ttf|otf|eot)$">
        Header always set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    
    # Images with long cache
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|ico|svg|avif)$">
        Header always set Cache-Control "public, max-age=31536000, immutable"
        Header always set Vary "Accept"
    </FilesMatch>
    
    # CORS for fonts
    <FilesMatch "\.(ttf|otf|woff|woff2|eot)$">
        Header always set Access-Control-Allow-Origin "*"
    </FilesMatch>
    
    # HTML files - no cache
    <FilesMatch "\.html$">
        Header always set Cache-Control "private, no-cache, no-store, must-revalidate"
        Header always set Pragma "no-cache"
        Header always set Expires "0"
    </FilesMatch>
</IfModule>

# -----------------------------------------------------------------------------
# SECURITY HEADERS
# -----------------------------------------------------------------------------
<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=(), usb=(), magnetometer=(), gyroscope=(), accelerometer=()"
    
    # Remove server signature
    Header always unset X-Powered-By
    Header unset X-Powered-By
</IfModule>

# -----------------------------------------------------------------------------
# MIME TYPES (if needed)
# -----------------------------------------------------------------------------
<IfModule mod_mime.c>
    AddType font/woff .woff
    AddType font/woff2 .woff2
    AddType application/font-woff .woff
    AddType application/font-woff2 .woff2
</IfModule>

# -----------------------------------------------------------------------------
# DENY ACCESS TO SENSITIVE FILES
# -----------------------------------------------------------------------------
# Deny access to dot files
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# Deny access to sensitive files
<FilesMatch "^(\.env|composer\.json|composer\.lock|package\.json|package-lock\.json|gulpfile\.js|webpack\.mix\.js)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# -----------------------------------------------------------------------------
# OPTIONS
# -----------------------------------------------------------------------------
# Disable directory browsing
Options -Indexes

# Follow symbolic links
Options +FollowSymLinks

# -----------------------------------------------------------------------------
# PHP SETTINGS (only if using mod_php, otherwise ignore)
# -----------------------------------------------------------------------------
<IfModule mod_php8.c>
    php_value upload_max_filesize 100M
    php_value post_max_size 100M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_value memory_limit 256M
</IfModule>
