# KSO Chandigarh - Main URL Rewriting Configuration
# This file handles clean URLs by hiding file extensions for the entire site

# Enable the rewrite engine
RewriteEngine On

# Set the base directory if needed
# If your site is in a subdirectory, uncomment and modify this:
# RewriteBase /

# Prevent direct access to this file
<Files .htaccess>
    Order Allow,Deny
    Deny from all
</Files>

# Prevent direct access to sensitive directories
RedirectMatch 403 ^/(config|logs|database|core)/.*$

# Don't rewrite if the file or directory exists
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Handle specific file types directly (don't rewrite)
RewriteCond %{REQUEST_URI} \.(css|js|jpg|jpeg|png|gif|ico|pdf|svg|woff|woff2|ttf|eot)$ [NC]
RewriteRule ^ - [L]

# Handle API requests directly (don't rewrite)
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^ - [L]

# Rewrite for PHP files (hiding .php extension)
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

# AdminLTE-specific redirects - handles adminlte/name to adminlte/name.php
RewriteCond %{REQUEST_URI} ^/adminlte/([^/\.]+)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/adminlte/$1.php -f
RewriteRule ^adminlte/([^/\.]+)$ adminlte/$1.php [L]

# Handle 404 errors
ErrorDocument 404 /404.php

# Security headers
<IfModule mod_headers.c>
    # Protect against XSS attacks
    Header set X-XSS-Protection "1; mode=block"
    # Prevent MIME-sniffing
    Header set X-Content-Type-Options "nosniff"
    # Referrer policy
    Header set Referrer-Policy "same-origin"
</IfModule>

# PHP settings
<IfModule mod_php7.c>
    # Maximum execution time
    php_value max_execution_time 300
    # Maximum file upload size
    php_value upload_max_filesize 20M
    php_value post_max_size 21M
    # Memory limit
    php_value memory_limit 256M
</IfModule>

# 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/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/x-javascript "access plus 1 month"
</IfModule>

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