RewriteEngine On
#RewriteBase /shinobiapps/public/
RewriteBase /

# Handle CORS for Firebase files
<IfModule mod_headers.c>
    <FilesMatch "^(firebase-messaging-sw\.js|firebase-config\.js)$">
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
        Header set Access-Control-Allow-Headers "Content-Type, X-CSRF-Token"
    </FilesMatch>
    
    # Handle OPTIONS preflight requests
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=200,L]
</IfModule>

# Explicit rules for firebase files
RewriteRule ^firebase-messaging-sw\.js$ - [L]
RewriteRule ^firebase-config\.js$ - [L]

# Add these rules to redirect to the public directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?path=/$1 [QSA,L]

# Additional security
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

# Deny access to .env and other sensitive files
<FilesMatch "\.(env|config|json|lock|git|md|gitignore|yml|yaml|log|sh)$">
    Require all denied
</FilesMatch>

# PHP error handling
php_flag display_errors off
php_value error_reporting E_ALL
php_value error_log ../storage/logs/webserver_errors.log

# Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    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/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# Disable Google PageSpeed
<IfModule pagespeed_module>
    ModPagespeed off
</IfModule>