# .htaccess (at root level)
################################################
# Basic Configuration for PHP Web Application
# Prevents directory listing
# Prevents access to sensitive files
# Enables URL rewriting for clean URLs
# Security headers
################################################

# Set the default directory index file
DirectoryIndex index.php index.html

# Enable URL Rewriting
RewriteEngine On

# Set the base URL for rewriting (IMPORTANT FIX)
# Change 'aims' to match your actual project folder name
RewriteBase /aims/

# Force HTTPS (Enable in production)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# Remove trailing slash (FIXED VERSION)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Prevent direct access to config and classes
RewriteRule ^(config|classes|includes)/ - [F,L]

# Block access to sensitive files
<FilesMatch "^\.(.*)$|^.*\.(env|log|sql|bak)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Custom Error Pages
ErrorDocument 404 /aims/error/404.php
ErrorDocument 403 /aims/error/403.php
ErrorDocument 500 /aims/error/500.php

# Set default charset
AddDefaultCharset UTF-8

# Enable Gzip 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 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"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    Header set X-XSS-Protection "1; mode=block"
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    # Enable in production with HTTPS
    # Header set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

# File Upload Limits (adjust as needed)
php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_execution_time 300
php_value max_input_time 300