# Kroply SEO & Performance Optimizations
# Enable GZIP Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/json
</IfModule>

# Browser Caching
<IfModule mod_expires.c>
  ExpiresActive On
  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 text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType text/html "access plus 1 week"
  ExpiresByType application/xhtml+xml "access plus 1 week"
</IfModule>

# Set Cache-Control headers
<IfModule mod_headers.c>
  # Cache images for 1 year
  <FilesMatch "\.(jpg|jpeg|png|gif|ico|svg|webp)$">
    Header set Cache-Control "max-age=31536000, public"
  </FilesMatch>
  
  # Cache CSS and JS for 1 month
  <FilesMatch "\.(css|js|woff2|woff)$">
    Header set Cache-Control "max-age=2592000, public"
  </FilesMatch>
  
  # Don't cache HTML, JSON files
  <FilesMatch "\.(html|json|xml)$">
    Header set Cache-Control "max-age=3600, public, must-revalidate"
  </FilesMatch>
  
  # Add security headers
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set X-XSS-Protection "1; mode=block"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Redirect HTTP to HTTPS
<IfModule mod_rewrite.c>
  RewriteEngine On
  
  # Single 301 permanent redirect for HTTP and/or WWW requests directly to https://kroply.in
  RewriteCond %{HTTPS} off [OR]
  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteRule ^(.*)$ https://kroply.in/$1 [L,R=301]

  # Consolidate legacy keyword aliases before clean-URL file rewrites.
  RewriteRule ^agriculture-shop-management-software/?$ /agricultural-software [L,R=301]
  RewriteRule ^ai-invoice-ocr/?$ /invoice-ocr [L,R=301]
  RewriteRule ^krishi-seva-kendra-software/?$ /krushi-seva-kendra-software [L,R=301]
  RewriteRule ^fertilizer-shop-software-demo/?$ /fertilizer-shop-software-free-trial [L,R=301]
  
  # Clean URL routing for static HTML landing pages and blog posts
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^(.*)$ $1.html [L]

  # SPA routing - route all non-file requests to index.html
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !^/sitemap.xml$
  RewriteCond %{REQUEST_URI} !^/robots.txt$
  RewriteCond %{REQUEST_URI} !^/llms\.txt$
  RewriteCond %{REQUEST_URI} !^/llms-full\.txt$
  RewriteCond %{REQUEST_URI} !^/ads\.txt$
  RewriteCond %{REQUEST_URI} !^/google.*\.html$
  RewriteRule ^ index.html [QSA,L]
</IfModule>

# Disable directory listing
Options -Indexes

# Protect sensitive files
<FilesMatch "^\.env|\.git|\.gitignore|composer.json|composer.lock|\.svn">
  Deny from all
</FilesMatch>
