Helicon Ape

mod_replace

Overview

mod_replace module allows to replace text strings based on regular expressions. It is generally used together with mod_proxy module to sanitize ill-behaving web-servers/applications (e.g. absolute links on web pages, absolute links in HTTP headers which aren't controlled by mod_proxy).

Quick start

Related articles and topics

Enviroment variables

Name Context Description
mod_replace S V D .h set this environment variable to value with name of defined filter
content-type S V D .h allows to explicitly specify the charset value for Content-Type header to be used by mod_replace

Examples

This is how you can enable simple_filter filter

ReplaceFilterDefine simple_filter
ReplacePattern simple_filter "aaaa" "bbbb"
ReplacePattern simple_filter "cccc" "dddd"

SetEnv mod_replace simple_filter

Note! Sometimes you'll need to explicitly specify the charset for mod_replace to use as some applications (like PHP) reset it bypassing IIS leaving Ape modules ignorant. To override Charset parameter value of Content-Type header use the following SetEnv line:

# sets Charset for mod_replace
SetEnv content-type windows-1251

Exact names of charsets may be taken from here .

Directives

Name Context Description
ReplaceFilterDefine S V D .h defines the name and parameters of the new filter
ReplacePattern S V D .h configures match and replace patterns for the filter using regular expressions
HeaderReplacePattern S V D .h configures regular-expressions-based match and replace patterns for the filter to alter response headers

ReplaceFilterDefine

ReplaceFilterDefine directive is used to specify the name and parameters of the filter.

Syntax

ReplaceFilterDefine name [options ...]
  • name - the name of the filter - is used to distinguish it from other filters.
  • options - defines some filter characteristics:
    • CaseIgnore - makes match pattern case-insensitive. If the options is not specified, comparison will be case-sensitive;
    • intype=<mime> - limits pattern matching to specific MIME-type only.

Example

#process text/html only
ReplaceFilterDefine revproxy CaseIgnore intype=text/html
ReplacePattern revproxy "(http|https)://origin.server/" "\1://revproxy/"
SetEnv mod_replace revproxy

Example

#process all responses with Case
ReplaceFilterDefine revproxy 
ReplacePattern revproxy "(http|https)://origin.server/" "\1://revproxy/"
SetEnv mod_replace revproxy

ReplacePattern

ReplacePattern directive specifies the match pattern and replacement string for the name filter.

Syntax

ReplacePattern name pattern string
  • name - the name of the filter specified in ReplaceFilterDefine directive.
  • pattern - a regular expression against which the HTTP body coming from the server will be matched.
  • string - a replacement string that will be used if the pattern matches.

Example

#process text/html only
ReplaceFilterDefine revproxy CaseIgnore intype=text/html
ReplacePattern revproxy "(http|https)://origin.server/" "\1://revproxy/"
SetEnv mod_replace revproxy

HeaderReplacePattern

HeaderReplacePattern directive specifies the match pattern and replacement string for the name filter to edit HTTP response header .

Syntax

HeaderReplacePattern name header pattern string
  • name - the name of the filter specified in ReplaceFilterDefine directive.
  • header - the name of HTTP response header to be edited.
  • pattern - a regular expression against which the HTTP body coming from the server will be matched.
  • string - a replacement string that will be used if the pattern matches.

Example

ReplaceFilterDefine revproxy CaseIgnore intype=text/html
HeaderReplacePattern revproxy Set-Cookie "domain=server.com" "domain=revproxy.com"

Note! To activate HeaderReplacePattern and RequestHeaderPattern filters SetOutputFilter is not necessary.

RequestHeaderPattern

RequestHeaderPattern directive specifies the match pattern and replacement string for the name filter to edit HTTP request header .

Syntax

RequestHeaderPattern header pattern string
  • header - the name of HTTP request header to be edited.
  • pattern - a regular expression against which the HTTP body coming from the server will be matched.
  • string - a replacement string that will be used if the pattern matches.

Example

RequestHeaderPattern Cookie " UID=0815" " UID=007"

Note! To activate HeaderReplacePattern and RequestHeaderPattern filters SetOutputFilter is not necessary.