summaryrefslogtreecommitdiffstats
path: root/docs/manual/mod/mod_substitute.xml
blob: 8404b9e67402a445ff4cf7a7990ab5522f50528a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?xml version="1.0"?>
<!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
<!-- $LastChangedRevision$ -->

<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<modulesynopsis metafile="mod_substitute.xml.meta">

<name>mod_substitute</name>
<description>Perform search and replace operations on response bodies</description>
<status>Extension</status>
<sourcefile>mod_substitute.c</sourcefile>
<identifier>substitute_module</identifier>

<summary>
    <p><module>mod_substitute</module> provides a mechanism to perform
    both regular expression and fixed string substitutions on
    response bodies.</p>
</summary>

<directivesynopsis>
<name>Substitute</name>
<description>Pattern to filter the response content</description>
<syntax>Substitute <var>s/pattern/substitution/[infq]</var></syntax>
<contextlist><context>directory</context>
<context>.htaccess</context></contextlist>
<override>FileInfo</override>
<compatibility>"expr=" substitution values were added in 2.5.1</compatibility>

<usage>
    <p>The <directive>Substitute</directive> directive specifies a
    search and replace pattern to apply to the response body.</p>

    <p>The meaning of the pattern can be modified by using any
    combination of these flags:</p>

    <dl>
        <dt><code>i</code></dt>
        <dd>Perform a case-insensitive match.</dd>
        <dt><code>n</code></dt>
        <dd>By default the pattern is treated as a regular expression.
        Using the <code>n</code> flag forces the pattern to be treated
        as a fixed string.</dd>
        <dt><code>f</code></dt>
        <dd>The <code>f</code> flag causes <code>mod_substitute</code> to flatten the
        result of a substitution allowing for later substitutions to
        take place on the boundary of this one. This is the default.</dd>
        <dt><code>q</code></dt>
        <dd>The <code>q</code> flag causes <code>mod_substitute</code> 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.</dd>
    </dl>

    <p>The <var>substitution</var> may contain literal text and regular
    expression backreferences. If the substitution begins with the text 
    <code>expr=</code> it is intepreted as an <a href="../expr.html">
    expression</a> which allows access to environment variables and
    header values.  </p>

    <example><title>Example</title>
    <highlight language="config">
&lt;Location "/"&gt;
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s/foo/bar/ni"
&lt;/Location&gt;
        </highlight>
    </example>

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

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

    <example><title>Example of using an alternate delimiter</title>
    <highlight language="config">
&lt;Location "/"&gt;
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s|&lt;BR */?&gt;|&lt;br /&gt;|i"
&lt;/Location&gt;
        </highlight>
    </example>

    <p>Backreferences can be used in the comparison and in the substitution,
    when regular expressions are used, as illustrated in the following example: </p>
    <example><title>Example of using backreferences and captures</title>
    <highlight language="config">
&lt;Location "/"&gt;
    AddOutputFilterByType SUBSTITUTE text/html
    # "foo=k,bar=k" -> "foo/bar=k"
    Substitute "s|foo=(\w+),bar=\1|foo/bar=$1|"
&lt;/Location&gt;
    </highlight>
    </example>

    <p> When using an <a href="../expr.html">expression</a> for the 
    <var>substitution</var>, regular expression backreferences must be 
    backslash ('\') escaped as illustrated in the example below:</p> 
    <example><title>Expression Example</title>
    <highlight language="config">
&lt;Location "/"&gt;
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s/example.com/expr=%{HTTP:HOST}/i"
    Substitute "s/Hello, (\S+)/expr=Hello from %{REQUEST_URI}, \$1/i"
&lt;/Location&gt;
        </highlight>
    </example>

    <note type="warning"><title>Expressions and caching</title>
    <p>Caution must be exercised when performing substitutions that reference
    HTTP request headers.  Because this module operates after response headers
    have been sent, the <a href="../expr.html">expression parser</a> cannot add
    referenced HTTP request headers to the outgoing Vary header. </p>
    </note>

    <p>A common use scenario for <code>mod_substitute</code> 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.</p>

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

    <example><title>Rewriting URLs embedded in proxied content</title>
    <highlight language="config">
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"
    </highlight>
    </example>

    <p><directive module="mod_proxy">ProxyPassReverse</directive>
    modifies any <code>Location</code> (redirect) headers that are sent
    by the back-end server, and, in this example,
    <directive>Substitute</directive> takes care of the rest of the problem by
    fixing up the HTML response as well.</p>

</usage>
</directivesynopsis>

<directivesynopsis>
<name>SubstituteMaxLineLength</name>
<description>Set the maximum line size</description>
<syntax>SubstituteMaxLineLength <var>bytes</var>(b|B|k|K|m|M|g|G)</syntax>
<default>SubstituteMaxLineLength 1m</default>
<contextlist><context>directory</context>
<context>.htaccess</context></contextlist>
<override>FileInfo</override>
<compatibility>Available in httpd 2.4.11 and later</compatibility>

<usage>
    <p>The maximum line size handled by <module>mod_substitute</module>
    is limited to restrict memory use. The limit can be configured
    using <directive>SubstituteMaxLineLength</directive>.
    The value can be given as the number of bytes and can be suffixed
    with a single letter <code>b</code>, <code>B</code>, <code>k</code>,
    <code>K</code>, <code>m</code>, <code>M</code>, <code>g</code>,
    <code>G</code> to provide the size in bytes, kilobytes, megabytes
    or gigabytes respectively.</p>

    <example><title>Example</title>
    <highlight language="config">
&lt;Location "/"&gt;
    AddOutputFilterByType SUBSTITUTE text/html
    SubstituteMaxLineLength 10m
    Substitute "s/foo/bar/ni"
&lt;/Location&gt;
        </highlight>
    </example>

</usage>
</directivesynopsis>

<directivesynopsis>
<name>SubstituteInheritBefore</name>
<description>Change the merge order of inherited patterns</description>
<syntax>SubstituteInheritBefore on|off</syntax>
<default>SubstituteInheritBefore on</default>
<contextlist><context>directory</context>
<context>.htaccess</context></contextlist>
<override>FileInfo</override>
<compatibility>Available in httpd 2.4.17 and later</compatibility>

<usage>
    <p>Whether to apply the inherited <directive module="mod_substitute">Substitute</directive>
    patterns first (<code>on</code>), or after the ones of the current
    context (<code>off</code>).
    The latter was the default in versions 2.4 and earlier, but changed
    starting with 2.5, hence <directive>SubstituteInheritBefore</directive>
    set to <code>off</code> allows to restore the legacy behaviour.
    <directive>SubstituteInheritBefore</directive> is itself inherited,
    hence contexts that inherit it (those that don't specify their own
    <directive>SubstituteInheritBefore</directive> value) will apply the
    closest defined merge order.</p>
</usage>
</directivesynopsis>

</modulesynopsis>