
RewriteEngine On

# Remove .php extension from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]


# Disable directory browsing
Options -Indexes

# Enable mod_rewrite
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Force WWW only (without HTTPS redirect)
    RewriteCond %{HTTP_HOST} ^3kwiki\.com$ [NC]
    RewriteRule ^ https://www.3kwiki.com%{REQUEST_URI} [R=301,L]

    # Remove .html from URLs (Redirect .html to clean URL)
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP [NC]
    RewriteRule ^(.+)\.html$ /$1 [R=301,L]

    # Serve .html files even if .html is not typed in URL
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html [L]

    # Redirect Trailing Slashes (except folders)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
</IfModule>

# Disable server signature
ServerSignature Off

# Enable Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript application/rss+xml application/vnd.ms-fontobject application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf
  AddOutputFilterByType DEFLATE application/xhtml+xml application/xml font/opentype font/otf font/ttf image/svg+xml image/x-icon
  AddOutputFilterByType DEFLATE text/css text/html text/javascript text/plain
</IfModule>

# Leverage Browser Caching
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access 1 year"
  ExpiresByType image/jpeg "access 1 year"
  ExpiresByType image/gif "access 1 year"
  ExpiresByType image/png "access 1 year"
  ExpiresByType image/webp "access 1 year"
  ExpiresByType text/css "access 1 year"
  ExpiresByType text/html "access 1 year"
  ExpiresByType application/pdf "access 1 year"
  ExpiresByType text/x-javascript "access 1 year"
  ExpiresByType application/x-shockwave-flash "access 1 year"
  ExpiresByType image/x-icon "access 1 year"
  ExpiresDefault "access 1 year"
</IfModule>

# CORS Headers for General Requests
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Authorization"

    # Cache images, CSS, JS, and fonts for one year
    <FilesMatch "\.(webp|avif|png|jpg|jpeg|gif|css|js|woff|woff2|svg|ico)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # Prevent indexing of specific file types
    <FilesMatch "\.(pdf|jpg|jpeg|png|gif|bmp|txt|doc|docx|xls|xlsx|ppt|pptx|csv|xml|json)$">
        Header set X-Robots-Tag "noindex, nofollow"
    </FilesMatch>

    # Cache-Control for different file types
    <FilesMatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf|webp)$">
        Header set Cache-Control "max-age=2678400, public"
    </FilesMatch>
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=7200, private, must-revalidate"
    </FilesMatch>
    <FilesMatch "\.(pdf)$">
        Header set Cache-Control "max-age=86400, public"
    </FilesMatch>
    <FilesMatch "\.(js)$">
        Header set Cache-Control "max-age=2678400, private"
    </FilesMatch>
</IfModule>