RewriteEngine On

# Redirect www to non-www dynamically
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

# Redirect HTTP to HTTPS dynamically
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteBase /

# Rewrite rules for specific pages
RewriteRule ^about-us/$ /pages/index.php?page=about-us.php [L]
RewriteRule ^contact-us/$ /pages/index.php?page=contact-us.php [L]
RewriteRule ^privacy-policy/$ /pages/index.php?page=privacy-policy.php [L]
RewriteRule ^disclaimer/$ /pages/index.php?page=disclaimers.php [L]
RewriteRule ^tearm-and-condition/$ /pages/index.php?page=tearm-and-condition.php [L]
RewriteRule ^sitemap.xml$ /sitemap.php [L]
RewriteRule ^pubish-now.php$ /pubish-now.php [L]

# Rule for ads.txt file
RewriteRule ^ads.txt$ /ads.txt [L]

# Add a trailing slash to URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

# Handle URLs with a trailing slash and single parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /index.php?q=$1 [L]

# Handle URLs with multiple parameters and trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /index.php?q=$1&page=$2 [L]
