Options -MultiViews
RewriteEngine On

# ─── إعادة التوجيهات ───────────────────────────────────
# تحويل index.php إلى /
RewriteCond %{THE_REQUEST} \s/index\.php[\s?] [NC]
RewriteRule ^index\.php$ / [R=301,L]

# تحويل index.html إلى /
RewriteCond %{THE_REQUEST} \s/index\.html[\s?] [NC]
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^browse\.html$ category.php [L,NC]
RewriteRule ^browse-(.*)-videos\.html$ category.php?cat=$1 [L,QSA,NC]
RewriteRule ^browse-(.*)-videos-([0-9]+)-(.*)\.html$ category.php?cat=$1&page=$2&sortby=$3 [L,QSA,NC]
RewriteRule ^videos\.(flv|mp4)(.*)$ videos.php$2 [L,NC]
RewriteRule ^(register|contact)\.html$ $1.php [L,NC,R=301]
RewriteRule ^contact\.html$ contact.php [L]

# ─── ضغط GZIP ───────────────────────────────────────────
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css text/javascript
  AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/json
  AddOutputFilterByType DEFLATE application/javascript application/x-javascript
  AddOutputFilterByType DEFLATE font/ttf font/otf font/woff font/woff2
  AddOutputFilterByType DEFLATE image/svg+xml image/webp
</IfModule>

# ─── دعم WebP للصور ─────────────────────────────────────
<IfModule mod_rewrite.c>
  RewriteCond %{HTTP_ACCEPT} image/webp [NC]
  RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
  RewriteRule ^(.*)\.(jpg|jpeg|png)$ $1.webp [T=image/webp,L]
</IfModule>

<IfModule mod_mime.c>
  AddType image/webp .webp
</IfModule>

# ─── التخزين المؤقت الذكي (Smart Cache) ─────────────────
<IfModule mod_expires.c>
  ExpiresActive On

  # لا كاش للصفحات الديناميكية
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType application/xhtml+xml "access plus 0 seconds"

  # كاش للملفات الثابتة
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  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 image/svg+xml "access plus 1 year"
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType audio/mpeg "access plus 1 year"
  ExpiresByType application/font-woff "access plus 1 year"
  ExpiresByType application/font-woff2 "access plus 1 year"
</IfModule>

# ─── حماية الهيدر والأمان ───────────────────────────────
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header always append X-Frame-Options SAMEORIGIN
  Header set X-XSS-Protection "1; mode=block"
  Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
  Header set Referrer-Policy "no-referrer-when-downgrade"

  # كاش لصور WebP
  <FilesMatch "\.webp$">
    Header set Cache-Control "max-age=31536000, public"
  </FilesMatch>
</IfModule>

# ─── إعادة توجيه إلى HTTPS ─────────────────────────────
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# ─── تصحيحات لمسارات خاطئة ─────────────────────────────
<IfModule mod_rewrite.c>
  RewriteRule ^series/series\.php$ /series.php [QSA,R=301,L]
</IfModule>

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor
<IfModule php8_module>
</IfModule>
<IfModule lsapi_module>
</IfModule>
# END cPanel-generated php ini directives, do not edit
