# .htaccess for Apache / LiteSpeed (TK Blinds)

# 1. Enable Rewrite Engine
RewriteEngine On
RewriteBase /

# 2. Block sensitive files and directories from direct HTTP access
RewriteRule ^(\.env|\.git|data/|config/|package.*\.json|composer.*|\.htaccess) - [F,L,NC]

# 3. Canonical 301 Redirects: Strip index.php and index.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html?)\ [NC]
RewriteRule ^index\.(php|html?)$ / [R=301,L]

# 4. Canonical 301 Redirects: Strip file extensions (.php and .html)
# Redirect /service/slug.php or /service/slug.html -> /service/slug
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /service/([a-zA-Z0-9_-]+)\.(php|html?)\ [NC]
RewriteRule ^service/([a-zA-Z0-9_-]+)\.(php|html?)$ /service/%1 [R=301,L]

# Redirect /page.php or /page.html -> /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9_-]+)\.(php|html?)\ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)\.(php|html?)$ /%1 [R=301,L]

# 5. Redirect Trailing Slashes if not a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

DirectoryIndex index.php 404.html

# 6. Clean Internal Rewrites to PHP Templates
# Admin Portal Routing
RewriteRule ^admin/?$ admin/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/admin/$1.php -f
RewriteRule ^admin/([a-zA-Z0-9_-]+)/?$ admin/$1.php [L,QSA]

# Blog & Guide Articles
RewriteRule ^blog/([a-zA-Z0-9_-]+)$ blog-post.php?slug=$1 [L,QSA]
RewriteRule ^blog/?$ blog.php [L,QSA]

# Service Detail Pages: /service/zebra-blinds -> service.php?slug=zebra-blinds
RewriteRule ^service/([a-zA-Z0-9_-]+)$ service.php?slug=$1 [L,QSA]

# Top-level Clean Pages: /about-us -> about-us.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L,QSA]

# API Process Quote Endpoint
RewriteRule ^api/process-quote$ api/process-quote.php [L,QSA]

# XML Sitemap
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# Custom 404 Error Document
ErrorDocument 404 /404.html

# 7. Security & Browser Caching Headers
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>

# 8. Gzip / Deflate Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
