# ─────────────────────────────────────────────────────
# AIB Agency — Production .htaccess Configuration
# Optimized for: https://yelazz.com/
# ─────────────────────────────────────────────────────

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # 1. Force HTTPS
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?yelazz\.com [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # 2. Block access to sensitive directories and dotfiles
    RewriteRule ^includes/ - [F,L]
    RewriteRule ^\.(env|git|htaccess|user\.ini) - [F,L]

    # 3. Legacy Redirects: /pages/buy and /pages/rent -> /pages/properties
    RewriteRule ^pages/buy(\.php)?$ /pages/properties [R=301,L]
    RewriteRule ^pages/rent(\.php)?$ /pages/properties [R=301,L]

    # 4. Strip .php extension from direct browser URL requests (301 Canonical URL enforcement)
    # Exclude POST requests so form submissions aren't stripped to GET
    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{THE_REQUEST} \s/([^\s?]+)\.php[\s?] [NC]
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]

    # 5. Image Backwards-Compatibility: assets/X.jpg -> assets/img/X.jpg
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^assets/([^/]+\.(jpg|jpeg|png|gif|webp|svg))$ assets/img/$1 [R=301,L]

    # 6. Pretty URL for Property & Article Details
    RewriteRule ^pages/property/([^/]+)/?$ pages/property.php?slug=$1 [QSA,L]
    RewriteRule ^property/([^/]+)/?$ pages/property.php?slug=$1 [QSA,L]
    RewriteRule ^pages/article/([^/]+)/?$ pages/article.php?slug=$1 [QSA,L]
    RewriteRule ^article/([^/]+)/?$ pages/article.php?slug=$1 [QSA,L]

    # 7. Admin Root Shortcut: /admin or /admin/ -> admin/index.php
    RewriteRule ^admin/?$ admin/index.php [L]

    # 8. Main Clean URL Rewrite & Fallback Router
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+)$ $1.php [L]

    # Catch-all router fallback for dynamic clean routes
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ router.php [L]

    # 9. Fallback Directory Index
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME}/index.php -f
    RewriteRule ^(.+)/?$ $1/index.php [L]
</IfModule>

# ─────────────────────────────────────────────────────
# Custom Error Pages
# ─────────────────────────────────────────────────────
ErrorDocument 404 /pages/404.php

# ─────────────────────────────────────────────────────
# Security & Performance 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"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ─────────────────────────────────────────────────────
# Browser Caching & Gzip Compression
# ─────────────────────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json image/svg+xml
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
</IfModule>
