# Enable RewriteEngine
RewriteEngine On

# Set home.php as the default index file
DirectoryIndex home.php

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

# Remove .php extension from URLs
# If the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# And the requested directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-d
# And a PHP file with that name exists
RewriteCond %{REQUEST_FILENAME}.php -f
# Then rewrite to add .php extension
RewriteRule ^(.+)$ $1.php [L,QSA]

# Redirect requests with .php extension to non-.php URL
# This prevents accessing both versions of the same page
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php(\?.*)?\ HTTP
RewriteRule (.*)\.php$ /$1 [R=301,L]

# Prevent directory browsing
Options -Indexes

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

# Enable compression for better performance
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Set caching headers for static resources
<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/javascript "access plus 1 month"
</IfModule>
