RewriteEngine On

# -----------------------------
# Legacy URL redirects (old site -> clean equivalents)
# -----------------------------
RewriteRule ^about-us/?$ /about [R=301,L]
RewriteRule ^services/.+$ /services [R=301,L]


# -----------------------------
# Custom 404 Error Page
# -----------------------------
ErrorDocument 404 /404.php

# -----------------------------
# Remove index.php from URL
# -----------------------------
RewriteCond %{THE_REQUEST} \s/+index\.php[\s?] [NC]
RewriteRule ^index\.php$ / [R=301,L]

# -----------------------------
# Redirect .php URLs to clean URLs (static pages)
# Only when accessed directly in browser (no internal rewrites)
# -----------------------------
RewriteCond %{THE_REQUEST} \s/+about\.php[\s?] [NC]
RewriteRule ^about\.php$ /about [R=301,L]

RewriteCond %{THE_REQUEST} \s/+blog\.php[\s?] [NC]
RewriteRule ^blog\.php$ /blog [R=301,L]

RewriteCond %{THE_REQUEST} \s/+contact\.php[\s?] [NC]
RewriteRule ^contact\.php$ /contact [R=301,L]

RewriteCond %{THE_REQUEST} \s/+courses\.php[\s?] [NC]
RewriteRule ^courses\.php$ /courses [R=301,L]

RewriteCond %{THE_REQUEST} \s/+career\.php[\s?] [NC]
RewriteRule ^career\.php$ /career [R=301,L]

RewriteCond %{THE_REQUEST} \s/+projects\.php[\s?] [NC]
RewriteRule ^projects\.php$ /projects [R=301,L]

RewriteCond %{THE_REQUEST} \s/+services\.php[\s?] [NC]
RewriteRule ^services\.php$ /services [R=301,L]

# -----------------------------
# Pretty URL redirect (dynamic - project details)
# -----------------------------
RewriteCond %{THE_REQUEST} \s/+project-details\.php\?project=([^\s&]+) [NC]
RewriteRule ^project-details\.php$ /project/%1? [R=301,L]

# -----------------------------
# Pretty URL redirect (dynamic - blog posts)
# -----------------------------
RewriteCond %{THE_REQUEST} \s/+details\.php\?post=([^\s&]+) [NC]
RewriteRule ^details\.php$ /post/%1? [R=301,L]

# -----------------------------
# Internally load .php files (static pages)
# Skip if file/directory actually exists
# -----------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^about/?$ about.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/?$ blog.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contact/?$ contact.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^courses/?$ courses.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^career/?$ career.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^projects/?$ projects.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^services/?$ services.php [L,QSA]

# -----------------------------
# Internally load dynamic pages
# -----------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([a-zA-Z0-9\-]+)/?$ project-details.php?project=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([a-zA-Z0-9\-]+)/?$ details.php?post=$1 [L,QSA]

# -----------------------------
# Dynamic sitemap — serve sitemap.php at /sitemap.xml
# (robots.txt points crawlers to /sitemap.xml)
# -----------------------------
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# -----------------------------
# Prevent llms.txt from downloading — serve as plain text
# -----------------------------
<Files "llms.txt">
    ForceType text/plain
    Header set Content-Type "text/plain; charset=UTF-8"
</Files>


# -----------------------------
# Gzip / Deflate compression (compresses HTML, CSS, JS, JSON, SVG, XML output)
# -----------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
    AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript text/javascript
    AddOutputFilterByType DEFLATE application/json application/ld+json
    AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>

# -----------------------------
# Browser caching (faster repeat loads, better Core Web Vitals)
# -----------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType video/mp4 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
</IfModule>

# -----------------------------
# Long-cache static assets; let HTML stay fresh
# -----------------------------
<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|jpg|jpeg|png|webp|svg|ico|woff2?|ttf|mp4)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
</IfModule>
