# ========================================================
# DrDirectory.pk - Apache / LiteSpeed / Hostinger Production Config
# ========================================================

# 0. Set Default Index Page
DirectoryIndex index.html index.php

# 1. Force UTF-8 Encoding for English & Urdu
AddDefaultCharset UTF-8
AddCharset UTF-8 .html .css .js .xml .json

# 2. Rewrite Engine & SEO Clean URLs (No .html)
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # A. Redirect HTTP to HTTPS (Automatic SSL)
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # B. External 301 Redirect: Strip .html extension from direct requests
  # If someone directly visits /doctors.html or /blog/slug.html, redirect them to /doctors or /blog/slug
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
  RewriteCond %{REQUEST_URI} !^/index\.html$ [NC]
  RewriteRule ^ /%1 [R=301,L]

  # C. Route /blog or /blog/ to health-blog.html (Blog Directory Hub)
  RewriteRule ^blog/?$ health-blog.html [L]

  # D. Route /blog/{slug} to /blog/{slug}.html
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/blog/$1.html -f
  RewriteRule ^blog/([^/]+)/?$ blog/$1.html [L]

  # D2. Fallback relative check for /blog/{slug}.html
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^ %{REQUEST_URI}.html [L]

  # E. Route extensionless root requests to .html (e.g. /doctors -> /doctors.html)
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/$1.html -f
  RewriteRule ^([^/]+)/?$ $1.html [L]

  # Fallback 404 to index.html
  ErrorDocument 404 /index.html
</IfModule>

# 3. Enable Gzip Compression for Fast Loading & SEO
<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/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# 4. Leverage Browser Caching
<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"
</IfModule>
