RewriteEngine On
RewriteBase /

# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Remove double slashes (//)
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ $0 [R=302,L,NE]

# Remove index.php or index.html
RewriteCond %{THE_REQUEST} ^.*/index\.(php|html?) [NC]
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,L]

# Remove .php from URL (but keep working)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# Custom 404 page
ErrorDocument 404 /404.php

# Cache Control for static files
<FilesMatch "\.(ico|pdf|flv|webp|jpeg|gif|js|css|swf|xml|woff|woff2|ttf|eot)$">
  Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType font/woff2 "access plus 4 months"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>