@@ -99,8 +99,19 @@ With this enabled, the string that you will need to match will look like ::
9999
100100 //host/path?query=bar
101101
102+ Note carefully that this includes the ``// `` prefix. This makes it possible to
103+ distinguish between the hostname and path components in cases where hostnames
104+ appear in the path. Consider, for example, a CDN that routes requests based on
105+ the origin hostname in the path, like this::
102106
103- A typical regex would look like ::
107+ https://cdn.example.com/origin1.example.com/some/path
108+
109+ The ``// `` prefix allows you to write a regex that reliably identifies the
110+ actual hostname versus a hostname-like string in the path. See the example
111+ for a demonstration of how to use the ``host `` option.
112+
113+ A typical regex, without the above mentioned method or host options, would look
114+ like::
104115
105116 ^/(ogre.*)/more http://www.ogre.com/$h/$0/$1
106117
@@ -171,3 +182,56 @@ as a redirect (Location:).
171182You can also combine multiple options, including overridable configs ::
172183
173184 ^/(.*)?$ https://example.com/sitemaps/$1 @proxy.config.url_remap.pristine_host_hdr=0 @status=301
185+
186+ Examples
187+ ~~~~~~~~
188+
189+ **Example: Basic path rewriting **
190+
191+ By default, regex_remap matches against the URL path and query string, which
192+ always starts with ``/ ``. This example rewrites requests from an old URL
193+ structure to a new one::
194+
195+ # remap.config:
196+ map http://www.example.com http://backend.example.com @plugin=regex_remap.so @pparam=rewrites.conf
197+
198+ # rewrites.conf:
199+ ^/old/(.*)$ http://backend.example.com/new/$1
200+
201+ A request to ``http://www.example.com/old/page.html `` will be rewritten to
202+ ``http://backend.example.com/new/page.html ``. The ``$1 `` substitution captures
203+ the first parenthesized group from the regex match.
204+
205+ **Example: HTTP to HTTPS redirect with host matching **
206+
207+ To redirect all HTTP requests to HTTPS while preserving the host and path,
208+ use the ``host `` option. With ``@pparam=host ``, the match string is
209+ ``//host/path?query `` (note the ``// `` prefix), so the following rule captures
210+ the entire string and redirects to HTTPS. Because the captured group includes
211+ the ``// ``, the substitution uses ``https: `` without ``// `` to avoid producing
212+ a malformed URL with four slashes::
213+
214+ # remap.config:
215+ regex_map http://(.*) https://unused.invalid @plugin=regex_remap.so @pparam=redirect.conf @pparam=host @pparam=pristine
216+
217+ # redirect.conf:
218+ ^(.*)$ https:$1 @status=307
219+
220+ This will redirect any plaintext HTTP (``http:// ``) request for any host to the
221+ ``https:// `` equivalent. For instance, a request to ``http://example.com/path ``
222+ would be redirected to ``https://example.com/path ``.
223+
224+ **Example: Method-based routing **
225+
226+ To route requests differently based on the HTTP method, use the ``method ``
227+ option. With ``@pparam=method ``, the match string is ``METHOD/path?query ``
228+ (note no space between method and path)::
229+
230+ # remap.config:
231+ map http://api.example.com http://backend.example.com @plugin=regex_remap.so @pparam=api.conf @pparam=method
232+
233+ # api.conf:
234+ ^GET/api/v1/(.*)$ http://read-replica.example.com/api/v1/$1
235+ ^POST/api/v1/(.*)$ http://write-primary.example.com/api/v1/$1
236+
237+ This routes GET requests to a read replica and POST requests to the primary.
0 commit comments