Mod_substitute - Apache HTTP Server Version 2.5

Substitute Directive ¶

Description:Pattern to filter the response content
Syntax:Substitute s/pattern/substitution/[infq]
Context:directory, .htaccess
Override:FileInfo
Status:Extension
Module:mod_substitute
Compatibility:"expr=" substitution values were added in 2.5.1

The Substitute directive specifies a search and replace pattern to apply to the response body.

The meaning of the pattern can be modified by using any combination of these flags:

i Perform a case-insensitive match. n By default the pattern is treated as a regular expression. Using the n flag forces the pattern to be treated as a fixed string. f The f flag causes mod_substitute to flatten the result of a substitution allowing for later substitutions to take place on the boundary of this one. This is the default. q The q flag causes mod_substitute to not flatten the buckets after each substitution. This can result in much faster response and a decrease in memory utilization, but should only be used if there is no possibility that the result of one substitution will ever match a pattern or regex of a subsequent one.

The substitution may contain literal text and regular expression backreferences. If the substitution begins with the text expr= it is interpreted as an expression which allows access to environment variables and header values.

Example

<Location "/"> AddOutputFilterByType SUBSTITUTE text/html Substitute "s/foo/bar/ni" </Location>

The character which is used to separate (or "delimit") the various parts of the substitution string is referred to as the "delimiter", and it is most common to use a slash for this purpose.

If either the pattern or the substitution contain a slash character then an alternative delimiter may be used to make the directive more readable:

Example of using an alternate delimiter

<Location "/"> AddOutputFilterByType SUBSTITUTE text/html Substitute "s|<BR */?>|<br />|i" </Location>

Backreferences can be used in the comparison and in the substitution, when regular expressions are used, as illustrated in the following example:

Example of using backreferences and captures

<Location "/"> AddOutputFilterByType SUBSTITUTE text/html # "foo=k,bar=k" -> "foo/bar=k" Substitute "s|foo=(\w+),bar=\1|foo/bar=$1|" </Location>

When using an expression for the substitution, regular expression backreferences must be backslash ('\') escaped as illustrated in the example below:

Expression Example

<Location "/"> AddOutputFilterByType SUBSTITUTE text/html Substitute "s/example.com/expr=%{HTTP:HOST}/i" Substitute "s/Hello, (\S+)/expr=Hello from %{REQUEST_URI}, \$1/i" </Location>

Expressions and caching

Caution must be exercised when performing substitutions that reference HTTP request headers. Because this module operates after response headers have been sent, the expression parser cannot add referenced HTTP request headers to the outgoing Vary header.

A common use scenario for mod_substitute is the situation in which a front-end server proxies requests to a back-end server which returns HTML with hard-coded embedded URLs that refer to the back-end server. These URLs don't work for the end-user, since the back-end server is unreachable.

In this case, mod_substitute can be used to rewrite those URLs into something that will work from the front end:

Rewriting URLs embedded in proxied content

ProxyPass "/blog/" "http://internal.blog.example.com/" ProxyPassReverse "/blog/" "http://internal.blog.example.com/" Substitute "s|http://internal.blog.example.com/|http://www.example.com/blog/|i"

ProxyPassReverse modifies any Location (redirect) headers that are sent by the back-end server, and, in this example, Substitute takes care of the rest of the problem by fixing up the HTML response as well.

Từ khóa » Html Br Substitute