# Fichier .htaccess pour StayManager
# Compatible local (XAMPP) et production


# Activer le moteur de réécriture
RewriteEngine On

# Rediriger vers HTTPS uniquement hors environnement local
# (localhost / 127.0.0.1 / ::1 ne doivent pas etre forces en HTTPS)
RewriteCond %{HTTP_HOST} !^(localhost|127\.0\.0\.1)(:\d+)?$ [NC]
RewriteCond %{REMOTE_ADDR} !^::1$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

# Definir l'environnement de production
# Peut etre surcharge via VirtualHost pour le local.
SetEnv APP_ENV production

# Page d'accueil - Redirection vers index.php si pas de fichier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/StayManager/?$
RewriteRule ^$ index.php [L]

# Routes principales - Accès direct aux fichiers PHP
# Les fichiers PHP sont accessibles directement (pas de réécriture nécessaire)
# Exemples :
# - /StayManager/login.php
# - /StayManager/dashboard_dynamic.php
# - /StayManager/reservations.php?action=create
# - /StayManager/restaurant_orders.php?action=show&id=1
# - etc.

# Proteger les fichiers de configuration
<FilesMatch "^(database|config)\.production\.php$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Proteger les fichiers sensibles
<FilesMatch "\.(log|sql|backup|env)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Proteger les dossiers sensibles
RedirectMatch 403 ^/StayManager/config/.*\.production\.php$
RedirectMatch 403 ^/StayManager/logs/.*
RedirectMatch 403 ^/StayManager/backups/.*
RedirectMatch 403 ^/StayManager/cache/.*
RedirectMatch 403 ^/StayManager/vendor/.*
RedirectMatch 403 ^/StayManager/database/.*\.sql$

# Desactiver l'affichage des repertoires
Options -Indexes

# Protection contre les injections
<IfModule mod_php7.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log "C:/xampp/htdocs/StayManager/logs/php_errors.log"
</IfModule>

<IfModule mod_php8.c>
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log "C:/xampp/htdocs/StayManager/logs/php_errors.log"
</IfModule>

# Compression GZIP
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Cache des fichiers statiques
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 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/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
</IfModule>

# Headers de sécurité
<IfModule mod_headers.c>
    # Protection XSS
    Header set X-XSS-Protection "1; mode=block"
    # Protection contre le clickjacking
    Header set X-Frame-Options "SAMEORIGIN"
    # Protection MIME type sniffing
    Header set X-Content-Type-Options "nosniff"
    # Politique de référent
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
