htmonitor

.htaccess File Documentation

Overview

This .htaccess file implements a sophisticated geo-redirection system for a WordPress website using LiteSpeed server. It automatically redirects users to country-specific subdirectories based on their geographic location while maintaining SEO-friendly URLs and proper caching mechanisms.

Table of Contents

  1. File Structure
  2. Section-by-Section Analysis
  3. Geo-Redirection Flow Diagram
  4. Country Codes and Mappings
  5. Technical Implementation Details
  6. LiteSpeed Cache Configuration
  7. WordPress Integration

File Structure

The .htaccess file is organized into several distinct sections:

๐Ÿ“ .htaccess Structure
โ”œโ”€โ”€ ๐Ÿšซ Exclusion Rules (Lines 3-19)
โ”œโ”€โ”€ ๐ŸŒ Geo-Redirection Rules (Lines 21-90)
โ”œโ”€โ”€ โšก LiteSpeed Cache Configuration (Lines 92-134)
โ”œโ”€โ”€ ๐Ÿ—‚๏ธ Browser Cache Settings (Lines 135-176)
โ”œโ”€โ”€ ๐Ÿ”ง WordPress Core Rules (Lines 178-192)
โ””โ”€โ”€ ๐Ÿš€ LiteSpeed Module Settings (Lines 193-200)

Section-by-Section Analysis

๐Ÿšซ Section 1: Exclusion Rules (Lines 3-19)

This section prevents certain URLs from being processed by the geo-redirection system.

Lines 3-4: WordPress Core File Protection

RewriteCond %{REQUEST_URI} ^/wp-load\.php$ [NC]
RewriteRule ^ - [L]

Explanation:

Lines 6-7: SEO Files Protection

RewriteCond %{REQUEST_URI} ^/robots\.txt$ [NC]
RewriteRule ^ - [L]

Explanation:

Lines 9-10: Sitemap Protection

RewriteCond %{REQUEST_URI} ^/sitemap_index\.xml$ [NC]
RewriteRule ^ - [L]

Explanation:

Lines 11-13: Commented Country Code Exclusion

# Exclude if URL contains a country code
# RewriteCond %{REQUEST_URI} ^/(au|at|ca|fr|de|ie|it|ch|es|uk|us|lu|li)(/|$) [NC]
# RewriteRule ^ - [L]

Explanation:

Lines 15-19: WordPress Admin Protection

# Exclude if URL contains wp-json or wp-admin
RewriteCond %{REQUEST_URI} (wp-json|wp-admin|wp-login|wp-content|wp-includes|wc-admin) [NC]
RewriteCond %{REQUEST_URI} !wc-api [NC]
RewriteCond %{REQUEST_URI} !wp-json [NC]
RewriteRule ^ - [L]

Explanation:


๐ŸŒ Section 2: Geo-Redirection Rules (Lines 21-90)

This is the core geo-redirection system that routes users based on their country.

Lines 21-23: USA (No Redirect)

# USA (no redirect, just pass through)
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$
RewriteRule ^ - [L]

Explanation:

Lines 25-29: Australia Redirection

# Australia
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$
RewriteCond %{REQUEST_URI} !^/(au|at|ca|fr|de|ie|it|ch|es|uk|us|lu|li|ch-fr|ch-it|ca-fr)(/|$) [NC]
RewriteCond %{REQUEST_URI} !wp-json [NC]
RewriteRule ^(.*)$ /au/$1 [R=302,L,QSA]

Explanation:

Pattern Repeats for Each Country:

Lines 79-84: UK & Default Fallback

# United Kingdom & Default
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^GB$ [OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(AU|AT|CA|FR|DE|IE|IT|CH|LI|ES|GB|US|LU)$
RewriteCond %{REQUEST_URI} !^/(au|at|ca|fr|de|ie|it|ch|es|uk|us|lu|li|ch-fr|ch-it|ca-fr)(/|$) [NC]
RewriteCond %{REQUEST_URI} !uk [NC]
RewriteRule ^(.*)$ /uk/$1 [R=302,L,QSA]

Explanation:

Lines 86-90: Empty GeoIP Fallback

# If no GEOIP_COUNTRY_CODE is set, redirect to /uk/ as default
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^$
RewriteCond %{REQUEST_URI} !^/(au|at|ca|fr|de|ie|it|ch|es|uk|us|lu|li|ch-fr|ch-it|ca-fr)(/|$) [NC]
RewriteCond %{REQUEST_URI} !wp-json [NC]
RewriteRule ^(.*)$ /uk/$1 [R=302,L,QSA]

Explanation:


โšก Section 3: LiteSpeed Cache Configuration (Lines 92-134)

Advanced caching configuration for LiteSpeed server.

Lines 94-99: Core Cache Settings

<IfModule LiteSpeed>
RewriteEngine on
CacheLookup on
RewriteRule .* - [E=Cache-Control:no-autoflush]
RewriteRule litespeed/debug/.*\.log$ - [F,L]
RewriteRule \.litespeed_conf\.dat - [F,L]

Explanation:

Lines 101-105: Async Processing

### marker ASYNC start ###
RewriteCond %{REQUEST_URI} /wp-admin/admin-ajax\.php
RewriteCond %{QUERY_STRING} action=async_litespeed
RewriteRule .* - [E=noabort:1]
### marker ASYNC end ###

Explanation:

Lines 107-110: Mobile Detection

### marker MOBILE start ###
RewriteCond %{HTTP_USER_AGENT} Mobile|Android|Silk/|Kindle|BlackBerry|Opera\ Mini|Opera\ Mobi [NC]
RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+ismobile]
### marker MOBILE end ###

Explanation:

### marker LOGIN COOKIE start ###
RewriteRule .? - [E="Cache-Vary:wp-wpml_current_language,,wp-postpass_4c6019f2292405368530c583968c4201"]
### marker LOGIN COOKIE end ###

Explanation:

Lines 116-121: WebP Image Support

### marker WEBP start ###
RewriteCond %{HTTP_ACCEPT} image/webp [OR]
RewriteCond %{HTTP_USER_AGENT} iPhone.*Version/(1[4-9]|[2-9][0-9]|[1-9][0-9]{2,}).*Safari [OR]
RewriteCond %{HTTP_USER_AGENT} Firefox/([6-9][0-9]|[1-9][0-9]{2,})
RewriteRule .* - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+webp]
### marker WEBP end ###

Explanation:

Lines 123-130: Query String Cleanup

### marker DROPQS start ###
CacheKeyModify -qs:fbclid
CacheKeyModify -qs:gclid
CacheKeyModify -qs:utm*
CacheKeyModify -qs:_ga
CacheKeyModify -qs:clickid
CacheKeyModify -qs:mpid
### marker DROPQS end ###

Explanation:


๐Ÿ—‚๏ธ Section 4: Browser Cache Settings (Lines 135-176)

Long-term browser caching for static assets.

Lines 138-172: File Type Expiration

<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType application/pdf A31557600
ExpiresByType image/x-icon A31557600
# ... (multiple file types)
</IfModule>

Explanation:


๐Ÿ”ง Section 5: WordPress Core Rules (Lines 178-192)

Standard WordPress rewrite rules.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Explanation:


๐Ÿš€ Section 6: LiteSpeed Module Settings (Lines 193-200)

Additional LiteSpeed configuration.

<IfModule Litespeed>
SetEnv noabort 1
</IfModule>

Explanation:


Geo-Redirection Flow Diagram

graph TD
    A[User Request] --> B{Is wp-load.php?}
    B -->|Yes| C[Allow - No Redirect]
    B -->|No| D{Is robots.txt?}
    D -->|Yes| C
    D -->|No| E{Is sitemap_index.xml?}
    E -->|Yes| C
    E -->|No| F{Is WordPress Admin/API?}
    F -->|Yes| C
    F -->|No| G{Country Code = US?}
    G -->|Yes| C
    G -->|No| H{Country Code = AU?}
    H -->|Yes| I{URL has country code?}
    I -->|Yes| C
    I -->|No| J[Redirect to /au/]
    H -->|No| K{Country Code = AT?}
    K -->|Yes| L{URL has country code?}
    L -->|Yes| C
    L -->|No| M[Redirect to /at/]
    K -->|No| N{Country Code = CA?}
    N -->|Yes| O{URL has country code?}
    O -->|Yes| C
    O -->|No| P[Redirect to /ca/]
    N -->|No| Q{Country Code = FR or LU?}
    Q -->|Yes| R{URL has country code?}
    R -->|Yes| C
    R -->|No| S[Redirect to /fr/]
    Q -->|No| T{Country Code = DE?}
    T -->|Yes| U{URL has country code?}
    U -->|Yes| C
    U -->|No| V[Redirect to /de/]
    T -->|No| W{Country Code = IE?}
    W -->|Yes| X{URL has country code?}
    X -->|Yes| C
    X -->|No| Y[Redirect to /ie/]
    W -->|No| Z{Country Code = IT?}
    Z -->|Yes| AA{URL has country code?}
    AA -->|Yes| C
    AA -->|No| BB[Redirect to /it/]
    Z -->|No| CC{Country Code = CH or LI?}
    CC -->|Yes| DD{URL has country code?}
    DD -->|Yes| C
    DD -->|No| EE[Redirect to /ch/]
    CC -->|No| FF{Country Code = ES?}
    FF -->|Yes| GG{URL has country code?}
    GG -->|Yes| C
    GG -->|No| HH[Redirect to /es/]
    FF -->|No| II{Country Code = GB or Unknown?}
    II -->|Yes| JJ{URL has country code?}
    JJ -->|Yes| C
    JJ -->|No| KK[Redirect to /uk/]
    II -->|No| LL{No GeoIP Data?}
    LL -->|Yes| MM{URL has country code?}
    MM -->|Yes| C
    MM -->|No| NN[Default Redirect to /uk/]
    
    style A fill:#e1f5fe
    style C fill:#c8e6c9
    style J fill:#fff3e0
    style M fill:#fff3e0
    style P fill:#fff3e0
    style S fill:#fff3e0
    style V fill:#fff3e0
    style Y fill:#fff3e0
    style BB fill:#fff3e0
    style EE fill:#fff3e0
    style HH fill:#fff3e0
    style KK fill:#fff3e0
    style NN fill:#ffcdd2

Country Codes and Mappings

Country Code Country/Region Redirect Path Notes
US United States No redirect Primary market
AU Australia /au/ ย 
AT Austria /at/ ย 
CA Canada /ca/ ย 
FR France /fr/ Also handles Luxembourg
LU Luxembourg /fr/ Shares French content
DE Germany /de/ ย 
IE Ireland /ie/ ย 
IT Italy /it/ ย 
CH Switzerland /ch/ Also handles Liechtenstein
LI Liechtenstein /ch/ Shares Swiss content
ES Spain /es/ ย 
GB United Kingdom /uk/ Also default fallback
Unknown Any other country /uk/ Default fallback
No GeoIP GeoIP failure /uk/ Safety net

Special Language Variants

The system also supports these language-specific paths (though not actively redirected to):


Technical Implementation Details

GeoIP Integration

Redirect Strategy

Cache Optimization

WordPress Integration


Performance Considerations

Cache Hit Rate Optimization

  1. Query Parameter Removal: Tracking parameters donโ€™t affect cache keys
  2. Mobile Separation: Different cache for mobile/desktop
  3. WebP Variants: Separate cache for modern browsers
  4. User Context: Logged-in users get personalized cache

SEO Benefits

  1. Country-Specific Content: Better local search rankings
  2. Proper Redirects: 302 redirects preserve link equity
  3. Robots.txt Protection: Search engines find sitemap correctly
  4. Clean URLs: Tracking parameters donโ€™t create duplicate content

Security Features

  1. Debug Log Protection: LiteSpeed logs are forbidden
  2. Config File Protection: Server configuration files blocked
  3. Admin Area Protection: WordPress admin remains functional

Troubleshooting Guide

Common Issues

  1. Infinite Redirects
    • Check if URL already contains country code
    • Verify exclusion rules are working
  2. WordPress Admin Issues
    • Ensure wp-admin exclusion rules are active
    • Check wp-json API accessibility
  3. Cache Problems
    • Verify LiteSpeed module is loaded
    • Check cache vary headers
  4. GeoIP Not Working
    • Confirm GeoIP module installation
    • Test with manual country code setting

Testing Commands

# Test geo-redirection
curl -I https://tradik.com/ -H "X-Forwarded-For: 1.2.3.4"

# Test with Google Bot
curl -I https://tradik.com/ -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"

# Test specific country
curl -I https://tradik.com/ -H "CF-IPCountry: AU"

Maintenance Notes

Regular Checks

Updates Required When


This documentation covers the complete .htaccess configuration for the tradik.com geo-redirection system. For technical support or modifications, ensure proper testing in a staging environment first.