# ==============================================================================
# National Green Care - Master Apache Configuration (.htaccess)
# ==============================================================================

ErrorDocument 404 /national-green-care/404.php

# 1. Prevent directory listing & Set default charset
Options -Indexes
AddDefaultCharset UTF-8

# 2. File Upload Limit Configuration (PHP settings)
<IfModule mod_php.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 12M
    php_value max_execution_time 300
    php_value max_input_time 300
</IfModule>

# 3. 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"
</IfModule>

# 4. Protect sensitive configuration and database files
<FilesMatch "^(config\.php|database\.php|\.env|\.htaccess|database\.sql|.*\.log|README\.md)$">
    Require all denied
</FilesMatch>

# 5. Rewrite Engine & Routing
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /national-green-care/

    # Block sensitive system directories and file extensions
    RewriteRule ^(config|database|includes)/.*$ - [F,L]
    RewriteRule \.(sql|env|log|bak|git)$ - [F,L]

    # --- Force Remove .php from URL (Redirect) ---
    # Redirect index.php to root directory (POST রিকোয়েস্ট ইগনোর করবে ডাটা সেভ করার জন্য)
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*)index\.php$ /national-green-care/$1 [R=301,L]

    # Redirect other .php files to extensionless URL (POST রিকোয়েস্ট ইগনোর করবে ডাটা সেভ করার জন্য)
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} \s/national-green-care/(.+)\.php\s [NC]
    RewriteRule ^(.+)\.php$ /national-green-care/$1 [R=301,L]

    # Clean URL Rewrites (Specific Routes)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^products/([a-zA-Z0-9_-]+)/?$ pages/product-details.php?slug=$1 [QSA,L]
    RewriteRule ^crop-solutions/?$ pages/crop-solutions.php [QSA,L]
    RewriteRule ^knowledge-center/?$ pages/knowledge.php [QSA,L]
    RewriteRule ^about/?$ pages/about.php [QSA,L]

    # Remove .php extension (Internal routing)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

# 6. Browser Caching for static assets
<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/webp "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>