# .htaccess para qr.dinhaparque.pt
# Otimizado para performance e SEO

# ============================================
# SEGURANÇA
# ============================================

# Desabilitar listagem de diretórios
Options -Indexes

# Proteger arquivos sensíveis
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

<FilesMatch "\.(env|log|ini|sh|sql|bak|conf)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Desabilitar execução de PHP em diretórios de upload
<Directory "videos">
    php_flag engine off
</Directory>

<Directory "images">
    php_flag engine off
</Directory>

# ============================================
# PERFORMANCE - COMPRESSÃO GZIP
# ============================================

<IfModule mod_deflate.c>
    # Comprimir HTML, CSS, JavaScript, Text, XML e fontes
    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
</IfModule>

# ============================================
# CACHE - CONTROLE DE EXPIRAÇÃO
# ============================================

<IfModule mod_expires.c>
    ExpiresActive On
    
    # Imagens
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/jpg "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"
    
    # Vídeos
    ExpiresByType video/mp4 "access plus 1 year"
    ExpiresByType video/webm "access plus 1 year"
    
    # CSS e JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    
    # Fontes
    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"
    
    # XML
    ExpiresByType application/xml "access plus 1 day"
    ExpiresByType text/xml "access plus 1 day"
    
    # Default
    ExpiresDefault "access plus 2 days"
</IfModule>

# ============================================
# CACHE - HEADERS
# ============================================

<IfModule mod_headers.c>
    # Cache para arquivos estáticos
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|ico|svg|css|js|woff|woff2|ttf|otf|mp4|webm)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
    
    # HTML não deve ser cacheado agressivamente
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=3600, must-revalidate"
    </FilesMatch>
    
    # XML
    <FilesMatch "\.(xml)$">
        Header set Cache-Control "max-age=86400, public"
    </FilesMatch>
    
    # Segurança adicional
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Remover server signature
    Header unset Server
    Header unset X-Powered-By
</IfModule>

# ============================================
# HTTPS REDIRECT (Se necessário)
# ============================================

# Descomente as linhas abaixo para forçar HTTPS
# <IfModule mod_rewrite.c>
#     RewriteEngine On
#     RewriteCond %{HTTPS} off
#     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# </IfModule>

# ============================================
# MIME TYPES
# ============================================

<IfModule mod_mime.c>
    # Vídeos
    AddType video/mp4 .mp4 .m4v
    AddType video/webm .webm
    
    # Fontes
    AddType font/ttf .ttf
    AddType font/otf .otf
    AddType font/woff .woff
    AddType font/woff2 .woff2
    
    # Outros
    AddType image/svg+xml .svg .svgz
    AddType image/webp .webp
</IfModule>

# ============================================
# PRETTY URLS (Opcional - Remover .html)
# ============================================

# Descomente para permitir URLs sem .html
# <IfModule mod_rewrite.c>
#     RewriteEngine On
#     RewriteCond %{REQUEST_FILENAME} !-f
#     RewriteCond %{REQUEST_FILENAME} !-d
#     RewriteRule ^([^\.]+)$ $1.html [NC,L]
# </IfModule>

# ============================================
# ERROR PAGES (Opcional)
# ============================================

# ErrorDocument 404 /404.html
# ErrorDocument 403 /403.html
# ErrorDocument 500 /500.html

# ============================================
# UTF-8 ENCODING
# ============================================

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