# Good Shepherd Center - Security Configuration
# Place this file in your website root directory

# ============================================
# SECURITY HEADERS
# ============================================

# Prevent clickjacking attacks
<IfModule mod_headers.c>
    # X-Frame-Options: Prevent site from being embedded in iframes
    Header always set X-Frame-Options "SAMEORIGIN"

    # X-Content-Type-Options: Prevent MIME-sniffing
    Header always set X-Content-Type-Options "nosniff"

    # X-XSS-Protection: Enable browser XSS protection
    Header always set X-XSS-Protection "1; mode=block"

    # Referrer-Policy: Control referrer information
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    # Permissions-Policy: Control browser features
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"

    # Strict-Transport-Security: Force HTTPS (uncomment after SSL is setup)
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</IfModule>

# ============================================
# FORCE HTTPS (Uncomment after SSL certificate is installed)
# ============================================
# <IfModule mod_rewrite.c>
#     RewriteEngine On
#     RewriteCond %{HTTPS} off
#     RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# </IfModule>

# ============================================
# CUSTOM ERROR PAGES
# ============================================
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
ErrorDocument 500 /404.html

# ============================================
# DISABLE DIRECTORY BROWSING
# ============================================
Options -Indexes

# ============================================
# PROTECT SENSITIVE FILES
# ============================================
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql|bak|conf)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ============================================
# GZIP COMPRESSION (Performance & Security)
# ============================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json
</IfModule>

# ============================================
# BROWSER CACHING (Performance)
# ============================================
<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/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType application/pdf "access plus 1 month"
</IfModule>

# ============================================
# URL REWRITING & SECURITY RULES
# ============================================
<IfModule mod_rewrite.c>
    RewriteEngine On

    # ============================================
    # REMOVE TRAILING SLASHES
    # ============================================

    # Remove trailing slashes (redirect /contact/ to /contact)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]

    # ============================================
    # REMOVE .HTML EXTENSIONS (Clean URLs)
    # ============================================

    # Redirect .html URLs to extensionless URLs
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
    RewriteRule ^(.*)\.html$ /$1 [R=301,L]

    # Internally rewrite extensionless URLs to .html files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html [L]

    # ============================================
    # BLOCK BAD BOTS & USER AGENTS
    # ============================================

    # Block suspicious user agents
    RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
    RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC]
    RewriteRule .* - [F,L]

    # ============================================
    # PREVENT SQL INJECTION
    # ============================================

    # Block query strings with SQL injection patterns
    RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR]
    RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR]
    RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)+ [NC,OR]
    RewriteCond %{QUERY_STRING} (union.*select.*\() [NC,OR]
    RewriteCond %{QUERY_STRING} (concat.*\() [NC]
    RewriteRule .* - [F,L]
</IfModule>

# ============================================
# MIME TYPE SECURITY
# ============================================
<IfModule mod_mime.c>
    # Prevent MIME-based attacks
    AddType text/html .html .htm
    AddType text/css .css
    AddType application/javascript .js
    AddType image/png .png
    AddType image/jpeg .jpg .jpeg
    AddType image/gif .gif
    AddType image/webp .webp
    AddType image/svg+xml .svg
    AddType application/pdf .pdf
</IfModule>
