[MINOR] redirect: in prefix mode a "/" means not to change the URI

If the prefix is set to "/", it means the user does not want to alter
the original URI, so we don't want to insert a new slash before the
original URI.

(cherry-picked from commit 02a35c74942c1bce762e996698add1270e6a5030)
This commit is contained in:
Willy Tarreau 2008-11-19 21:15:17 +01:00
parent 0140f2553c
commit fe651a50d6
3 changed files with 14 additions and 6 deletions

View File

@ -2350,7 +2350,10 @@ redirect prefix <to> [code <code>] <option> {if | unless} <condition>
the HTTP "Location" header. In case of "redirect prefix", the
"Location" header is built from the concatenation of <to> and the
complete URI, including the query string, unless the "drop-query"
option is specified (see below).
option is specified (see below). As a special case, if <to>
equals exactly "/" in prefix mode, then nothing is inserted
before the original URI. It allows one to redirect to the same
URL.
<code> The code is optional. It indicates which type of HTTP redirection
is desired. Only codes 301, 302 and 303 are supported, and 302 is

View File

@ -1912,9 +1912,14 @@ int http_process_request(struct session *s, struct buffer *req)
if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
goto return_bad_req;
/* add prefix */
memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
rdr.len += rule->rdr_len;
/* add prefix. Note that if prefix == "/", we don't want to
* add anything, otherwise it makes it hard for the user to
* configure a self-redirection.
*/
if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
rdr.len += rule->rdr_len;
}
/* add path */
memcpy(rdr.str + rdr.len, path, pathlen);

View File

@ -26,8 +26,8 @@ listen sample1
redirect prefix /pfx/test code 302 if url_test2
redirect prefix /pfx/test code 303 drop-query if url_test3
redirect location /test4 code 302 set-cookie SEEN=1 if url_test4 !seen
redirect location / code 302 clear-cookie SEEN= if url_test4 seen
redirect prefix / code 302 set-cookie SEEN=1 if url_test4 !seen
redirect location / code 302 clear-cookie SEEN= if url_test4 seen
### unconditional redirection
#redirect location https://example.com/ if TRUE