Skip to content

Rewrite

Rewrite rules execute natively on both stock and patched OpenLiteSpeed. On patched OLS (“Full” edition) the LSI-API fast path runs; on stock OLS the module’s built-in engine executes an Apache mod_rewrite subset directly — no patch required.

DirectiveSyntax
RewriteEngineRewriteEngine On|Off
RewriteBaseRewriteBase /path/
RewriteCondRewriteCond TestString Pattern [flags]
RewriteRuleRewriteRule Pattern Substitution [flags]
RewriteOptionsRewriteOptions inherit|IgnoreInherit
RewriteMapRewriteMap name txt|rnd|int:/path
  • PCRE2 regular expressions (the same engine used everywhere in the module, with match_limit protection against pathological patterns)
  • . matches newlines too (Apache rewrite semantics)
  • A leading ! negates the pattern — note that negated matches carry no capture groups for $N substitution
FormBehaviour
-URI unchanged (use with [F]/[G] or condition-only rules)
/pathInternal rewrite
/path?queryInternal rewrite with new query string
http(s)://…External redirect
relativePrefixed with RewriteBase

Expansions inside the substitution:

  • $0 … $9 — capture groups from the rule pattern
  • %{VAR} — server variables (same set as RewriteCond, below)
FlagEffect
[L], [END]Stop processing further rules
[R] / [R=301..308]External redirect (default 302 when bare)
[NC]Case-insensitive pattern
[QSA]Append the original query string
[F]403 Forbidden (use - as substitution)
[G]410 Gone
[NE], [PT]Accepted, no-op in this subset

Test strings may reference %{VAR} server variables:

%{REQUEST_URI}, %{REQUEST_FILENAME}, %{QUERY_STRING}, %{HTTP_HOST}, %{HTTPS}, %{REMOTE_ADDR}, %{REQUEST_METHOD}, %{HTTP_USER_AGENT}, %{HTTP_REFERER}, %{HTTP:Header}, %{ENV:var}

CondPattern forms:

FormMeaning
PCRE2 regexMatches the expanded test string
!patternNegation
-f / -d / -sFile exists / is directory / is non-empty (relative paths resolve against the docroot)
< > =Lexicographic comparison with the literal operand

Cond flags: [NC], [OR] (an OR-group is satisfied when any member matches; the default combines conditions with AND).

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteRule ^/old/(.*)$ /new/$1 [L]
RewriteEngine On
RewriteRule ^/secret - [F]
  • Rules run once in file order; after an internal rewrite the chain is not restarted (Apache’s per-directory context re-scans from the top). The dominant real-world pattern — a single catch-all front controller — behaves identically.
  • $N back-references inside RewriteCond test strings are approximated as empty until the first rule has matched.
  • %N cond-group references inside substitutions are reserved but not filled in v1.

OLS ships native MaxMind MMDB support and injects GEOIP_* variables into the request environment; this engine’s %{ENV:var} reads them directly. One-time server config (httpd_config.conf, exact syntax verified against the OLS source):

geoipDB /usr/local/share/GeoLite2-Country.mmdb {
geoipDBName COUNTRY_DB
maxminddbenv GEOIP_COUNTRY_CODE COUNTRY_DB/country/iso_code
}

Then country rules are plain rewrites:

RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(US|DE)$ [NC]
RewriteRule .* - [F]

Verified live: GB-addressed request → 403, US → 200, no module changes required.