# Email Verifier Pro - Apache Rewrite Rules
# Place this file in the SAME folder as api/index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # If installed in a subfolder (e.g., /api/), set the path:
    RewriteBase /api/
    
    # Redirect all requests to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

# CORS Headers - MUST be before any blocking rules
<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-API-Key, X-License-Key, X-Requested-With"
    Header always set Access-Control-Expose-Headers "Content-Type, X-RateLimit-Remaining"
    Header always set Access-Control-Max-Age "86400"
    
    # Add headers to all responses including errors
    Header always set Access-Control-Allow-Origin "*" env=IS_CORS
</IfModule>

# Handle OPTIONS preflight
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=204,L]
</IfModule>

# PHP Settings
<IfModule mod_php.c>
    php_value memory_limit 256M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_value post_max_size 64M
    php_value upload_max_filesize 64M
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log "logs/php-errors.log"
</IfModule>

<IfModule mod_php8.c>
    php_value memory_limit 256M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_value post_max_size 64M
    php_value upload_max_filesize 64M
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log "logs/php-errors.log"
</IfModule>

# Deny access to sensitive files and folders
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

<Files "config.php">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</Files>

# Also protect these folders if accessed directly
RedirectMatch 403 ^/(lib|middleware|config)/.*$

# Allow all common HTTP methods
<IfModule mod_allowmethods.c>
    AllowMethods GET POST PUT DELETE OPTIONS
</IfModule>
