]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_substitute.xml
xforms
[apache] / docs / manual / mod / mod_substitute.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <modulesynopsis metafile="mod_substitute.xml.meta">
24
25 <name>mod_substitute</name>
26 <description>Perform search and replace operations on response bodies</description>
27 <status>Extension</status>
28 <sourcefile>mod_substitute.c</sourcefile>
29 <identifier>substitute_module</identifier>
30
31 <summary>
32     <p><module>mod_substitute</module> provides a mechanism to perform
33     both regular expression and fixed string substitutions on
34     response bodies.</p>
35 </summary>
36
37 <directivesynopsis>
38 <name>Substitute</name>
39 <description>Pattern to filter the response content</description>
40 <syntax>Substitute <var>s/pattern/substitution/[infq]</var></syntax>
41 <contextlist><context>directory</context>
42 <context>.htaccess</context></contextlist>
43 <override>FileInfo</override>
44
45 <usage>
46     <p>The <directive>Substitute</directive> directive specifies a
47     search and replace pattern to apply to the response body.</p>
48
49     <p>The meaning of the pattern can be modified by using any
50     combination of these flags:</p>
51
52     <dl>
53         <dt><code>i</code></dt>
54         <dd>Perform a case-insensitive match.</dd>
55         <dt><code>n</code></dt>
56         <dd>By default the pattern is treated as a regular expression.
57         Using the <code>n</code> flag forces the pattern to be treated
58         as a fixed string.</dd>
59         <dt><code>f</code></dt>
60         <dd>The <code>f</code> flag causes mod_substitute to flatten the
61         result of a substitution allowing for later substitutions to
62         take place on the boundary of this one. This is the default.</dd>
63         <dt><code>q</code></dt>
64         <dd>The <code>q</code> flag causes mod_substitute to not
65         flatten the buckets after each substitution. This can
66         result in much faster response and a decrease in memory
67         utilization, but should only be used if there is no possibility
68         that the result of one substitution will ever match a pattern
69         or regex of a subsequent one.</dd>
70     </dl>
71
72     <example><title>Example</title>
73     <highlight language="config">
74 &lt;Location /&gt;
75     AddOutputFilterByType SUBSTITUTE text/html
76     Substitute s/foo/bar/ni
77 &lt;/Location&gt;
78         </highlight>
79     </example>
80
81     <p>If either the pattern or the substitution contain a slash
82     character then an alternative delimiter should be used:</p>
83
84     <example><title>Example of using an alternate delimiter</title>
85     <highlight language="config">
86 &lt;Location /&gt;
87     AddOutputFilterByType SUBSTITUTE text/html
88     Substitute "s|&lt;BR */?&gt;|&lt;br /&gt;|i"
89 &lt;/Location&gt;
90         </highlight>
91     </example>
92
93     <p>Backreferences can be used in the comparison and in the substitution,
94     when regular expressions are used, as illustrated in the following example: </p>
95     <example><title>Example of using backreferences and captures</title>
96     <highlight language="config">
97 &lt;Location /&gt;
98     AddOutputFilterByType SUBSTITUTE text/html
99     # "foo=k,bar=k" -> "foo/bar=k" 
100     Substitute "s|foo=(\w+),bar=\1|foo/bar=$1"
101 &lt;/Location&gt;
102     </highlight>
103     </example>
104
105     <p>A common use scenario for <code>mod_substitute</code> is the
106     situation in which a front-end server proxies requests to a back-end
107     server which returns HTML with hard-coded embedded URLs that refer
108     to the back-end server. These URLs don't work for the end-user,
109     since the back-end server is unreachable.</p>
110
111     <p>In this case, <code>mod_substutite</code> can be used to rewrite
112     those URLs into something that will work from the front end:</p>
113
114     <example><title>Rewriting URLs embedded in proxied content</title>
115     <highlight language="config">
116 ProxyPass /blog/ http://internal.blog.example.com
117 ProxyPassReverse /blog/ http://internal.blog.example.com/
118
119 Substitute "s|http://internal.blog.example.com/|http://www.example.com/blog/|i"
120     </highlight>
121     </example>
122
123     <p><directive module="mod_proxy">ProxyPassReverse</directive>
124     modifies any <code>Location</code> (redirect) headers that are sent
125     by the back-end server, and, in this example,
126     <code>Substitute</code> takes care of the rest of the problem by
127     fixing up the HTML response as well.</p>
128
129 </usage>
130 </directivesynopsis>
131
132 </modulesynopsis>