RewriteEngine On

# Allow direct access for administrative and static directories
RewriteRule ^(admin|assets|uploads|payment|marketplace/api|api)(/.*)?$ - [L,NC]

# If the request points to an existing file or directory, serve it directly
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Redirect legacy .php URLs (except protected directories) to their SEO counterparts
RewriteCond %{THE_REQUEST} \s/+([^.\s?]+)\.php[\s?] [NC]
RewriteCond %{REQUEST_URI} !^/epin/admin/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/assets/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/uploads/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/payment/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/marketplace/api/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/api/ [NC]
RewriteRule ^ %1 [R=301,L]

# Route legacy .php files through the router (except protected directories)
RewriteCond %{REQUEST_URI} !^/epin/admin/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/assets/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/uploads/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/payment/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/marketplace/api/ [NC]
RewriteCond %{REQUEST_URI} !^/epin/api/ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php$ [NC]
RewriteRule ^ router.php [L,QSA]

# All remaining requests handled by router
RewriteRule ^(.*)$ router.php [L,QSA]
# Apache Configuration for Digital Shop
# (Merged with main rewrite rules above)

# Security Headers
<IfModule mod_headers.c>
    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"
</IfModule>

# Disable Directory Browsing
Options -Indexes +FollowSymLinks

# Protect config files
<FilesMatch "^(config\.php|database\.sql)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# PHP Settings
<IfModule mod_php7.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_reporting 22527
</IfModule>

<IfModule mod_php8.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_reporting 22527
</IfModule>

# Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript 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 text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>

# Error Pages
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php
