# ----------------------------------------------------------------------
# 1. ENGINES & CORE SETTINGS
# ----------------------------------------------------------------------
RewriteEngine On
Options -Indexes

# ----------------------------------------------------------------------
# 2. CANONICAL REDIRECTS & FOLDER SECURITY
# ----------------------------------------------------------------------

# BLOCK internal folders first to prevent unnecessary redirects
RewriteRule ^(admin|images|extra|PHPMailer|phpmailer)/ - [F,L]

# Force HTTPS and remove www.
#RewriteCond %{HTTPS} off [OR]
#RewriteCond %{HTTP_HOST} ^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
#RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE] 

# Redirect /index and /index/ to root
RewriteCond %{THE_REQUEST} \s/+index[\s/?] [NC]
RewriteRule ^index[/]?$ / [R=301,L]

# Remove trailing slash from URLs (except for actual directories)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

# ----------------------------------------------------------------------
# 3. URL REWRITING (EXTENSIONS & ROUTING)
# ----------------------------------------------------------------------

# If you have a front controller pattern (like index.php handling all routes)
# Uncomment the lines below if your site uses a single entry point

# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ index.php?/$1 [L]

# Blog detail page routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9_-]+)/?$ template-blog-detail.php?blog=$1 [L,QSA]

# Sitemap routing
RewriteRule ^sitemap\.xml$ sitemap.xml.php [L]
RewriteRule ^sitemap/?$ sitemap.php [L]

# Internally map clean URLs to .html files (SKIP blog URLs)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

# General URL routing catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?url=$1 [QSA,L]

# ----------------------------------------------------------------------
# 4. BOT & SCRAPER BLOCKING
# ----------------------------------------------------------------------
SetEnvIfNoCase User-Agent "(Seekport|ZoominfoBot|CCBot|Bytespider|PetalBot)" bad_bot
SetEnvIfNoCase User-Agent "(HTTrack|WebCopier|SiteSucker|WebZip|Teleport|Offline Explorer)" bad_bot
SetEnvIfNoCase User-Agent "(MJ12bot|AhrefsBot|SemrushBot|DotBot|BLEXBot|DataForSeoBot|GPTBot|ChatGPT-User|ClaudeBot)" bad_bot

<Limit GET POST HEAD>
    Order Allow,Deny
    Allow from all
    Deny from env=bad_bot
</Limit>

# ----------------------------------------------------------------------
# 5. PERFORMANCE: COMPRESSION
# ----------------------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/javascript application/json application/xml font/woff2 font/woff image/svg+xml
</IfModule>

# ----------------------------------------------------------------------
# 6. PERFORMANCE: BROWSER CACHING
# ----------------------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
    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"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# ----------------------------------------------------------------------
# 7. SECURITY & SEO HEADERS
# ----------------------------------------------------------------------
<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=63072000; includeSubDomains"
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header set X-XSS-Protection "0"
    
    # Static Assets
    <FilesMatch "\.(js|css|webp|jpg|jpeg|png|gif|ico|svg)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    
    # Fonts (with CORS support)
    <FilesMatch "\.(woff|woff2|ttf|eot|svg)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

    # Dynamic Content
    <FilesMatch "\.(html|php)$">
        Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>
</IfModule>

# ----------------------------------------------------------------------
# 8. SENSITIVE FILE PROTECTION & ERRORS
# ----------------------------------------------------------------------
<FilesMatch "^(\.htaccess|\.htpasswd|\.git|composer\.json|composer\.lock|package\.json|\.env|config\.php|README\.md)$">
    Require all denied
</FilesMatch>

<FilesMatch "\.(bak|config|sql|log|sh|inc|swp|dist|backup|old)$">
    Require all denied
</FilesMatch>

ErrorDocument 404 /404
ErrorDocument 403 /403
ErrorDocument 500 /500
# DO NOT REMOVE THIS LINE AND THE LINES BELOW IPALLOWID:HzRWpRW7y9
allow from 122.129.74.186
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE HzRWpRW7y9:IPALLOWID

